关于如何解决这些警告,你有三个选项:
- 守护调用 。你可以使用OperatingSystem.IsWindows(),在调用 API 之前检查你是否在 Windows 上运行 。
- 将调用标记为 Windows 专属 。在某些情况下,可以通过[SupportedOSPlatform("windows")]将调用成员标记为平台专属 。
- 删除代码 。通常这不是你想要的,因为这意味着 Windows 用户使用你的代码时会损失一些内容,但是对于存在跨平台替代方案的情况,你最好放弃那些平台专属的代码 。例如,可以使用 XML 配置文件来替代注册表 。
- 禁止警告 。你当然可以通过.editorconfig或#pragmawarningdisable来作弊,简单地禁用警告 。但是,在使用特定于平台的 API 时,你应该选择选项(1)和(2) 。
private static string GetLoggingDirectory(){if (OperatingSystem.IsWindows()){using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SoftwareFabrikam")){if (key?.GetValue("LoggingDirectoryPath") is string configuredPath)return configuredPath;}}string exePath = Process.GetCurrentProcess().MainModule.FileName;string folder = Path.GetDirectoryName(exePath);return Path.Combine(folder, "Logging");}
要将代码标记为 Windows 专属,请应用新的SupportedOSPlatform属性:[SupportedOSPlatform("windows")]private static string GetLoggingDirectory(){using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SoftwareFabrikam")){if (key?.GetValue("LoggingDirectoryPath") is string configuredPath)return configuredPath;}string exePath = Process.GetCurrentProcess().MainModule.FileName;string folder = Path.GetDirectoryName(exePath);return Path.Combine(folder, "Logging");}
在这两种情况下,使用注册表的警告都会消失 。关键区别在于,在第二个示例中,分析器现在将针对GetLoggingDirectory()的调用站点发出警告,因为它现在被视为 Windows 特定的 API 。换句话说,你将进行平台检查的要求转发给了调用者 。
[SupportedOSPlatform]属性可以应用于成员、类型或程序集级别 。BCL 本身也使用此属性 。例如,程序集Microsoft.Win32.Registry应用了此属性,所以分析器能先知道注册表是 Windows 特定的 API 。
请注意,如果你以net5.0-windows为目标,此属性将自动应用于你的程序集 。这意味着从net5.0-windows使用 Windows 专属的 API 永远不会产生任何警告,因为你的整个程序集都被视为 Windows 专属的 。
处理 Blazor WebAssembly 中不支持的 APIBlazor WebAssembly 项目在浏览器沙箱中运行,这限制了你可以使用的 API 。例如,尽管线程和进程创建都是跨平台的 API,但我们无法使这些 API 在 Blazor WebAssembly 中运行,这意味着它们会抛出PlatformNotSupportedException警告 。我们已经用[UnsupportedOSPlatform("browser")]标记了这些 API 。
假设,你将GetLoggingDirectory()方法复制并粘贴到一个 Blazor WebAssembly 应用程序中 。
private static string GetLoggingDirectory(){//...string exePath = Process.GetCurrentProcess().MainModule.FileName;string folder = Path.GetDirectoryName(exePath);return Path.Combine(folder, "Logging");}
你会收到以下警告:CA1416 'Process.GetCurrentProcess()' is unsupported on 'browser'CA1416 'Process.MainModule' is unsupported on 'browser'
为了处理这些警告,你的选项和处理 Windows 特定的 API 时基本相同 。你可以守护调用:
private static string GetLoggingDirectory(){//...if (!OperatingSystem.IsBrowser()){string exePath = Process.GetCurrentProcess().MainModule.FileName;string folder = Path.GetDirectoryName(exePath);return Path.Combine(folder, "Logging");}else{return string.Empty;}}
或者,你可以将成员标记为 Blazor WebAssembly 不支持的成员:[UnsupportedOSPlatform("browser")]private static string GetLoggingDirectory(){//...string exePath = Process.GetCurrentProcess().MainModule.FileName;string folder = Path.GetDirectoryName(exePath);return Path.Combine(folder, "Logging");}
由于浏览器沙箱的限制非常严格,因此不能预期所有类库和 NuGet 包都能在 Blazor WebAssembly 中工作 。此外,绝大多数库也不一定能在 Blazor WebAssembly 中运行 。因此,目标为net5.0的常规类库不会看到 Blazor WebAssembly 不支持的 API 警告 。你必须通过在项目文件中添加项,来显式声明你打算在 Blazor WebAssembly 中支持你的项目:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net5.0</TargetFramework></PropertyGroup><ItemGroup><SupportedPlatform Include="browser" /></ItemGroup></Project>
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- .net core IIS部署教程
- 为什么中国火器到清朝就停止发展了 清朝为何不发展火器
- 基于.NET Core+Bootstrap的快速后台开发框架
- 基于.NET Core的Orchard Core框架出来了
- 自媒体|趣头条宣布停止自媒体平台:6月28日后账户余额清零
- .Net在Windows上使用Jenkins做CI/CD
- ppt背景音乐怎么中途暂停后继续播放?ppt音乐怎么中途停止?
- .NET CORE HttpClient使用
- C语言标准库的7类函数
- 巧克力|这类常见的零食立即停止食用!或导致中毒