Search code examples
c#.netmsbuild.net-5

CA1416. How to tell builder that only platform is Windows?


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?


Solution

  • 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>