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?

poke
  • 369,085
  • 72
  • 557
  • 602
filimonic
  • 3,988
  • 2
  • 19
  • 26

2 Answers2

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>
JL0PD
  • 3,698
  • 2
  • 15
  • 23
  • 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 '21 at 11:56
  • @TK-421, you can read about it in [docs](https://learn.microsoft.com/en-us/dotnet/standard/analyzers/platform-compat-analyzer) – JL0PD Oct 19 '21 at 12:10
  • I've read this whole article that you linked, but can't find an answer anywhere. I am making an ASP.NET website that will be hosted on a Windows IIS and uses a Windows-specific method. Does that mean that users using Linux can't access my website, or everything will be okay because the app runs on a windows? – TK-421 Oct 19 '21 at 12:19
  • 2
    @TK-421, this attribute is only a recommendation from compiler to developer. It won't affect users of website – JL0PD Oct 19 '21 at 12:37
  • 12
    Last one does nothing (testing with .NET 6). – Nicke Manarin Nov 10 '21 at 23:47
  • 12
    @NickeManarin do you have `false` somewhere in project or `Directory.build.props`? – JL0PD Nov 11 '21 at 01:24
  • @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. – Nicke Manarin Nov 11 '21 at 22:38
  • See also https://stackoverflow.com/questions/71097598/how-to-globally-specify-android-and-ios-are-not-supported-in-a-net-5-0-app/71106689#71106689 for more specific details on how `..` is handled – Jeremy Lakeman Mar 17 '22 at 03:52
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>

Joseph Wambura
  • 2,732
  • 2
  • 21
  • 24