28

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
  • 1
    Change Target OS in properties for project (Console, REST API,...) ?
    – Kiquenet
    Jul 13, 2022 at 6:56

2 Answers 2

55

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>
8
  • 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
    Oct 19, 2021 at 11:56
  • @TK-421, you can read about it in docs
    – JL0PD
    Oct 19, 2021 at 12:10
  • 2
    @TK-421, this attribute is only a recommendation from compiler to developer. It won't affect users of website
    – JL0PD
    Oct 19, 2021 at 12:37
  • 13
    Last one does nothing (testing with .NET 6). Nov 10, 2021 at 23:47
  • 12
    @NickeManarin do you have <GenerateAssemblyInfo>false</GenerateAssemblyInfo> somewhere in project or Directory.build.props?
    – JL0PD
    Nov 11, 2021 at 1:24
4
  1. Removed the AssemblyInfo.cs file.

  2. Removed the following from the .csproj file

    < GenerateAssemblyInfo>false</ GenerateAssemblyInfo>

  3. Add to the .csproj file

    <PropertyGroup>
    
        <TargetFramework>net8.0-windows</TargetFramework>
    
        <PlatformName>windows</PlatformName>
    
        <OutputType>WinExe</OutputType>"
    

    </ Project>

1
  • Great answer. Fixed everything for me. I used it in my .Net 7 app with <TargetFramework>net7.0-windows</TargetFramework> Mar 18 at 17:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.