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

3

39

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?

Cookery answered 15/4, 2021 at 23:6 Comment(1)
Change Target OS in properties for project (Console, REST API,...) ?Headmaster
P
69

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>
Petey answered 16/4, 2021 at 4:7 Comment(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?Exemplum
@TK-421, you can read about it in docsPetey
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?Exemplum
@TK-421, this attribute is only a recommendation from compiler to developer. It won't affect users of websitePetey
Last one does nothing (testing with .NET 6).Urfa
@NickeManarin do you have <GenerateAssemblyInfo>false</GenerateAssemblyInfo> somewhere in project or Directory.build.props?Petey
@Petey Yes, I have. Removing it fixed the issue. Of course, I just had to migrate the app details (version, names, etc) to the project.Urfa
See also #71098098 for more specific details on how <TargetFramework>.. is handledKallick
C
13
  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>

Chita answered 18/3, 2023 at 6:57 Comment(3)
Great answer. Fixed everything for me. I used it in my .Net 7 app with <TargetFramework>net7.0-windows</TargetFramework>Florettaflorette
funny thing: WinExe will discard all your logs if you're launching your exe as a child process AHAHA; thanksVoluptuous
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.Clouded
D
0

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

Disposition answered 22/5 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.