Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 656
filimonic Avatar asked Apr 15 '21 23:04

filimonic


1 Answers

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>
like image 136
JL0PD Avatar answered Nov 13 '22 10:11

JL0PD