Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet tools targeting Windows APIs #12055

Open
Tracked by #29436
wli3 opened this issue Jun 12, 2020 · 17 comments
Open
Tracked by #29436

dotnet tools targeting Windows APIs #12055

wli3 opened this issue Jun 12, 2020 · 17 comments

Comments

@wli3
Copy link

wli3 commented Jun 12, 2020

Due to this issue, WPF and Winforms net5.0 dotnet tools are not available

What happens if a dotent tools targeting net5.0-win and want to call Windows specific API? Say access Bluetooth to help debugging a device.

Currently the target framework selection is choosing the most compatible TFM according to the TFM SDK is built on. In this case, SDK will be built on net5.0, and it won't choose net5.0-win.

This need external dependency. We need runtime team to provide us a mechanism to tell if 5.0 windows runtime is install on the machine, which we've been asked a long time.

@wli3 wli3 self-assigned this Jun 12, 2020
@wli3
Copy link
Author

wli3 commented Jun 12, 2020

cc @KathleenDollard

@wli3
Copy link
Author

wli3 commented Jun 12, 2020

at the same time I don't think we have net5.0-linux and net5.0-mac.

@wli3
Copy link
Author

wli3 commented Jun 12, 2020

my opinion is this is a low priority. I don't think there are many tools want to access platform specific API. We could ship without this support and see the reaction.

@marcpopMSFT
Copy link
Member

dotnet/runtime#46128

@tthiery
Copy link

tthiery commented May 12, 2021

It would be lovely if this makes it into the .NET 6 release ;). As in the other issue described, I have exactly the bluetooth case described in the original description.

@wli3 wli3 assigned joeloff and unassigned wli3 Sep 28, 2021
@wli3 wli3 modified the milestones: 6.0.1xx, 6.0.2xx Sep 28, 2021
@marcpopMSFT marcpopMSFT modified the milestones: 6.0.2xx, 7.0.1xx Jan 5, 2022
@guillaume86
Copy link

I would also use this, my use case is showing Toast notifications if the platform allows it.

@JanKotschenreuther
Copy link

JanKotschenreuther commented Jan 14, 2022

We also got use cases.

For example we got cli tools which got large lists of options as input.
Because of the size of these commands, we keep those in scripts.
We actually build UI tools to help managing those scripts, keeping track of and changing all options and their states.

@lindexi
Copy link
Member

lindexi commented Aug 3, 2022

How can I ignore the NETSDK1146 error?

@lindexi
Copy link
Member

lindexi commented Aug 3, 2022

How can I ignore the NETSDK1146 error?

One of the hack code (Beaking in .NET 8):

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

Update in #12055 (comment)

@marcpopMSFT marcpopMSFT modified the milestones: 7.0.1xx, Backlog Dec 7, 2022
@dsplaisted dsplaisted changed the title dotnet tools X net5.0 dotnet tools targeting Windows APIs Apr 18, 2023
@LazaroOnline
Copy link

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

Pardon my ignorance, but where should I put that code?
I tried in csproj like this:

<Project Sdk="Microsoft.NET.Sdk">
  ...
  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    ....
  </Target>
</Project>

but didn't work. It throws an error "MSB4232 Items that are outside Target elements must have one of the following operations: Include, Update, or Remove."

So I suppose I need to create some other file related to msbuild, but I'm not familiar with that process.
Please, can someone explain how to add that Target hack code?

@KalleOlaviNiemitalo
Copy link
Contributor

@LazaroOnline did you place the Target element within an ItemGroup element. I expect that would cause the MSB4232 error. If you did, move it out. If you didn't, please show in more detail what you did.

@LazaroOnline
Copy link

LazaroOnline commented Jun 18, 2023

@KalleOlaviNiemitalo yes, I also tried placing it into an ItemGroup and had the same error message.
Here is a more detailed example of the csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <PackAsTool>True</PackAsTool>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
  </ItemGroup>

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
      <PropertyGroup>
        <TargetPlatformIdentifier></TargetPlatformIdentifier>
      </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
      <PropertyGroup>
        <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
      </PropertyGroup>
    </Target>

</Project>

Error:
Items that are outside Target elements must have one of the following operations: Include, Update, or Remove.

@KalleOlaviNiemitalo
Copy link
Contributor

The Target elements should not be inside the ItemGroup element.

@LazaroOnline
Copy link

LazaroOnline commented Jun 18, 2023

@KalleOlaviNiemitalo True, I got it to work without the ItemGroup, however VisualStudio seems to have some stability problems handling these manual changes, it intermittently complains about the csproj.
I will edit my example above to leave a working example so it is less confusing for others.

Anyway, even if I could pack it as a nuget dotnet-tool, and publish it to nuget.org, it fails when the user tries to install, since in my case I only have 1 target net7.0-windows which is invalid.

@JL03-Yue JL03-Yue removed their assignment Apr 19, 2024
@Anduin2017
Copy link
Member

Anduin2017 commented May 23, 2024

How can I ignore the NETSDK1146 error?

One of the hack code:

    <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier></TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

    <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation">
        <PropertyGroup>
            <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
        </PropertyGroup>
    </Target>

At @lindexi I tired adding this hack.

However when I was installing it, I got an error:

$ dotnet tool install MyApp--global
Tool 'xxxxxxxxxxxxxxxx.xxxxx' failed to update due to the following:
The settings file in the tool's NuGet package is invalid: Settings file 'DotnetToolSettings.xml' was not found in the package.
Tool 'xxxxxxxxxxxxxxxx.xxxxx' failed to install. Contact the tool author for assistance.

I tried to download the nupkg and unzip it locally, but found DotnetToolSettings.xml locally.

image

lindexi added a commit to lindexi/lindexi_gd that referenced this issue May 23, 2024
@lindexi
Copy link
Member

lindexi commented May 23, 2024

@Anduin2017 Update the hack csproj code for .NET 8.0 or greater

  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    <PropertyGroup>
      <TargetPlatformIdentifier></TargetPlatformIdentifier>
      <TargetPlatformMoniker></TargetPlatformMoniker>
    </PropertyGroup>
  </Target>

  <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation" BeforeTargets="PackTool">
    <PropertyGroup>
      <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
    </PropertyGroup>
  </Target>

All the csproj code:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows</TargetFramework>
    <UseWPF>true</UseWPF>
    <PackAsTool>true</PackAsTool>
    <ToolCommandName>lindexi</ToolCommandName>
  </PropertyGroup>

  <Target Name="HackBeforePackToolValidation" BeforeTargets="_PackToolValidation">
    <PropertyGroup>
      <TargetPlatformIdentifier></TargetPlatformIdentifier>
      <TargetPlatformMoniker></TargetPlatformMoniker>
    </PropertyGroup>
  </Target>

  <Target Name="HackAfterPackToolValidation" AfterTargets="_PackToolValidation" BeforeTargets="PackTool">
    <PropertyGroup>
      <TargetPlatformIdentifier>Windows</TargetPlatformIdentifier>
    </PropertyGroup>
  </Target>

</Project>

And you can find all my code in https://github.com/lindexi/lindexi_gd/blob/fdd3959ce65cf2ebeead9706c04901caafb1f89c/WinemwhajallawLigawakuja/WinemwhajallawLigawakuja.csproj

@Anduin2017
Copy link
Member

Thank you @lindexi ! I tried your hack an it works! Now I can distribute my WPF app via dotnet tool!

gsuuon added a commit to gsuuon/grab that referenced this issue May 25, 2024
dotnet PackAsTool is busted since dotnet 5.0 if using windowsforms or wpf (though I definitely had it working with dotnet 7). Not sure the right way to do this, I don't like bringing in windowsforms just to get screen resolution and offsets but i've already spent way too long trying to find another way and there doesn't seem to be one. maybe wmi/cim would work, but i couldn't find if it can report the offsets necessary for ffmpeg to record the screen correctly.

I think this workaround basically lies to packastool when it does its check. doesn't seem to have any impact on if it works or not at runtime.

see dotnet/sdk#16361 and dotnet/sdk#12055
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests