不支持在面向 .NET 5 或更高版本时直接引用 Windows 元数据组件

Posted

技术标签:

【中文标题】不支持在面向 .NET 5 或更高版本时直接引用 Windows 元数据组件【英文标题】:Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported 【发布时间】:2021-12-07 20:00:06 【问题描述】:

我有一个 .Net5.0 C# 类库项目,它构建得很好,但是在将目标框架更改为 <TargetFramework>net6.0</TargetFramework> 后,构建开始失败并显示以下错误消息:

C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.AI.MachineLearning.MachineLearningContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Activation.ActivatedEventsContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Activation.ActivationCameraSettingsContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Activation.ContactActivatedEventsContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Activation.WebUISearchActivatedEventsContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Background.BackgroundAlarmApplicationContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Calls.Background.CallsBackgroundContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Calls.CallsPhoneContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Calls.CallsVoipContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Calls.LockScreenCallContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.CommunicationBlocking.CommunicationBlockingContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.FullTrustAppContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Preview.InkWorkspace.PreviewInkWorkspaceContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Preview.Notes.PreviewNotesContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Resources.Management.ResourceIndexerContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Search.Core.SearchCoreContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
C:\Program Files\dotnet\sdk\6.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.targets(1003,5): error NETSDK1130: Windows.ApplicationModel.Search.SearchContract.winmd cannot be referenced. Referencing a Windows Metadata component directly when targeting .NET 5 or higher is not supported. For more information, see https://aka.ms/netsdk1130
...

现在即使将配置恢复为<TargetFramework>net5.0</TargetFramework>,问题仍然存在。请注意,我正在使用 Visual Studio 2019 并已安装 .Net6.0 SDK。这是csproj 文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Ben.Demystifier" Version="0.1.4" />
    <PackageReference Include="BundlerMinifier.Core" Version="3.2.449" />
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.6.1" />
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.AzureAD.UI" Version="2.1.1" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.30.1" />
    <PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="4.5.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
    <PackageReference Include="System.Runtime.Serialization.Formatters" Version="4.3.0" />
    <PackageReference Include="Synergix.WE.ComputerManagement" Version="2.4.34" />
  </ItemGroup>

  <ItemGroup>

更新:

&lt;PackageReference Include="Synergix.WE.ComputerManagement" Version="2.4.34" /&gt; 是使用.netStandard2.1 构建的,当从引用中删除它时,错误消失了,但是由于内部依赖,构建失败,是否不可能在.net6 项目中引用.netStandard 库?

【问题讨论】:

【参考方案1】:

解决方案:

1 - 将 TargetFramework 设置为 &lt;TargetFramework&gt;net6.0-windows$([Microsoft.Build.Utilities.ToolLocationHelper]::GetLatestSDKTargetPlatformVersion('Windows', '10.0'))&lt;/TargetFramework&gt;

2 - 添加对 CsWinRT&lt;PackageReference Include="Microsoft.Windows.CsWinRT" Version="1.4.1" /&gt; 的包引用

【讨论】:

以上是关于不支持在面向 .NET 5 或更高版本时直接引用 Windows 元数据组件的主要内容,如果未能解决你的问题,请参考以下文章

Error:(162, 111) java: -source 1.5 中不支持方法引用 (请使用 -source 8 或更高版本以启用方法引用)的解决方法

eclipse gradle插件Buildship  不支持gradle6.5或更高版本了吗?

maven报错:Error:java: 不再支持源选项 5。请使用 6 或更高版本。

Maven编译项目时报错:不再支持源选项 5。请使用 6 或更高版本。 不再支持目标选项 1.5。请使用 1.6 或更高版本。

异步函数仅在面向 ECMAScript 2015 或更高版本时可用

不再支持源选项 5。使用 6 或更高版本的 Android Studio