49

dotnet run (on windows) causes warning CA1416: This call site is reachable on all platforms. 'WellKnownSidType.WorldSid' is only supported on: 'windows'.

My program is designed to run only on windows.

I tried to add SupportedPlatform to MyApp.csproj, but no luck.

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="5.0.5" />
    <PackageReference Include="System.DirectoryServices" Version="5.0.0" />
    <SupportedPlatform Include="Windows"/>
  </ItemGroup>

</Project>

What am I doing wrong? How can I show dotnet run that this project is windows-only?

1
  • 2
    Change Target OS in properties for project (Console, REST API,...) ? Commented Jul 13, 2022 at 6:56

3 Answers 3

85

You can mark each windows-specific method with System.Runtime.Versioning.SupportedOSPlatformAttribute e.g.

[SupportedOSPlatform("windows")]
public static void Foo()
    => Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

Mark entire assembly with in AssemblyInfo (if you have one)

[assembly: System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]

Or edit .csproj file to target windows-specific version of dotnet to make these changes automatic

<TargetFramework>net5.0-windows</TargetFramework>
Sign up to request clarification or add additional context in comments.

9 Comments

Does the SupportedOSPlatform revers to the machine on which the dotnet app is being run, or the os platforms that user can reach the app from?
@TK-421, this attribute is only a recommendation from compiler to developer. It won't affect users of website
Last one does nothing (testing with .NET 6).
@NickeManarin do you have <GenerateAssemblyInfo>false</GenerateAssemblyInfo> somewhere in project or Directory.build.props?
@JL0PD Yes, I have. Removing it fixed the issue. Of course, I just had to migrate the app details (version, names, etc) to the project.
|
17
  1. Removed the AssemblyInfo.cs file.
  2. Removed the following from the .csproj file
    <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
  1. Add to the .csproj file
    <Project Sdk="Microsoft.NET.Sdk.Web">
        <PropertyGroup>
            <TargetFramework>net8.0-windows</TargetFramework>
            <PlatformName>windows</PlatformName>
            <OutputType>WinExe</OutputType>
       </PropertyGroup>
    </Project>

3 Comments

Great answer. Fixed everything for me. I used it in my .Net 7 app with <TargetFramework>net7.0-windows</TargetFramework>
funny thing: WinExe will discard all your logs if you're launching your exe as a child process AHAHA; thanks
The whole point of <GenerateAssemblyInfo>false</GenerateAssemblyInfo> is to get rid of that dreaded AssemblyInfo.cs. Certainly don't want to disable this new feature just because of this little glitch.
4

For getting by just in the code just wrap the function that is MS only in check for OperationSystem, like:

if (OperatingSystem.IsWindows())
    // call to a service that is only suported on Windows
else
    throw new Exception("Only available on Windows operating system!");

If it helps sombody...

2 Comments

This works, but if you have a lot of places this is done then the attribute from the other answer is nice. Especially if the app is only planning to target windows.
@ps2goat: If you are planning to only target Windows, you are right. Otherwise, using the attribute form just moves the problem elsewhere, to wherever the now attributed method is called. For multi-platform applications, at some point, you'll have to fork execution between those lines that will run only on Windows and what is alternatively going to happen if you're not on Windows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.