【问题标题】:CA1416. How to tell builder that only platform is Windows?CA1416。如何告诉 builder 唯一的平台是 Windows?
【发布时间】:2021-04-15 23:06:10
【问题描述】:

dotnet run(在 Windows 上)原因 warning CA1416: This call site is reachable on all platforms. 'WellKnownSidType.WorldSid' is only supported on: 'windows'.

我的程序设计为只能在 Windows 上运行。

我尝试将SupportedPlatform 添加到MyApp.csproj,但没有成功。

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

我做错了什么?如何向dotnet run 表明此项目仅适用于 Windows?

【问题讨论】:

    标签: c# .net msbuild .net-5


    【解决方案1】:

    您可以使用System.Runtime.Versioning.SupportedOSPlatformAttribute 标记每个特定于 Windows 的方法,例如

    [SupportedOSPlatform("windows")]
    public static void Foo()
        => Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
    

    在 AssemblyInfo 中标记整个程序集(如果有的话)

    [assembly: System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
    

    或编辑 .csproj 文件以针对特定于 Windows 的 dotnet 版本,以使这些更改自动进行

    <TargetFramework>net5.0-windows</TargetFramework>
    

    【讨论】:

    • SupportedOSPlatform 是否返回到运行 dotnet 应用程序的机器,或用户可以访问应用程序的操作系统平台?
    • @TK-421,你可以在docs阅读它
    • @TK-421,这个属性只是编译器给开发者的建议。不会影响网站用户
    • 最后一个什么都不做(使用 .NET 6 进行测试)。
    • @NickeManarin 你在项目的某处有&lt;GenerateAssemblyInfo&gt;false&lt;/GenerateAssemblyInfo&gt; 还是Directory.build.props
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 2020-09-18
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多