【问题标题】:Compile error after adding .net6 nuget packages to .net6 project将 .net6 nuget 包添加到 .net6 项目后编译错误
【发布时间】:2022-06-11 00:13:37
【问题描述】:

我有 Visual Studio 2022 版本 17.1.6、.net Sdk 版本 6.0.202,此外我还安装了其他几个 .net 版本,包括核心 3.1.0、3.1.22、3.1.24,我重新安装了所有其中,在可用时安装运行时和 SDK

我有这个问题:我使用“新项目”向导创建了一个针对 .net6 的新 c# 控制台应用程序,它运行了。

然后我在 6.x 版中添加任何 Microsoft NuGet 包(比如说 Microsoft.EntityFrameworkCore),我在错误列表中看到:

错误 MSB4057:项目中不存在目标“NETStandardCompatError_System_Runtime_CompilerServices_Unsafe_netcoreapp3_1”。

如果我构建解决方案,输出中会出现同样的错误。

我用引用压缩了项目并将其发送给同事,它在他的电脑上编译。

我尝试添加对 EntityFrameworkCore 版本 5 的引用,它运行良好。 我对其他 nuget 包也有同样的问题:版本 6 触发 MSB4057 错误,版本 5 有效。

System.Runtime.CompilerServices.Unsafe 在我的项目中没有作为直接引用出现,它实际上被其他程序集间接引用,例如 Microsoft.Externsions.Caching.Memory、Microsoft.Extensions.Options 等。

在解决方案资源管理器中,我正确地看到了引用,没有“危险”图标和路径 C:\Users\lucav.nuget\packages\system.runtime.compilerservices.unsafe\6.0.0 实际上包含dll

在 Nuget 我找不到 system.runtime.compilerservices.unsafe 3.1

我用 Winmerge 检查了没有引用的香草 hello world 和添加了引用的区别,结果如下:

.csProj

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
</ItemGroup>

project.assets.json 复制在解决方案资源管理器中看到的引用结构,它包含对“System.Runtime.CompilerServices.Unsafe”的引用:“6.0.0”

比如

      "System.Runtime.CompilerServices.Unsafe/6.0.0": {
        "type": "package",
        "compile": {
          "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll": {}
        },
        "runtime": {
          "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll": {}
        },
        "build": {
 

     "buildTransitive/netcoreapp2.0/System.Runtime.CompilerServices.Unsafe.targets": {}
    }
  

最重要的是,这个引用了 netcoreapp3.1

"System.Runtime.CompilerServices.Unsafe/6.0.0": {
      "sha512": "/iUeP3tq1S0XdNNoMz5C9twLSrM/TH+qElHkXWaPvuNOt+99G75NrV0OS2EqHx5wMN7popYjpc8oTjC1y16DLg==",
      "type": "package",
      "path": "system.runtime.compilerservices.unsafe/6.0.0",
      "files": [
        ".nupkg.metadata",
        ".signature.p7s",
        "Icon.png",
        "LICENSE.TXT",
        "THIRD-PARTY-NOTICES.TXT",
        "buildTransitive/--netcoreapp3.1/_._",
        "buildTransitive/netcoreapp2.0/System.Runtime.CompilerServices.Unsafe.targets",
        "lib/net461/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/net461/System.Runtime.CompilerServices.Unsafe.xml",
        "lib/net6.0/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/net6.0/System.Runtime.CompilerServices.Unsafe.xml",
        **"lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.dll",**
        "lib/netcoreapp3.1/System.Runtime.CompilerServices.Unsafe.xml",
        "lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dll",
        "lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.xml",
        "system.runtime.compilerservices.unsafe.6.0.0.nupkg.sha512",
        "system.runtime.compilerservices.unsafe.nuspec",
        "useSharedDesignerContext.txt"
      ]
    }

最后是文件 *.csproj.nuget.g.targets

没有nuget包的版本是:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>
</Project>

添加对 EntityFrameworkCore 的引用后就变成了

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
    <Import Project="$(NuGetPackageRoot)system.runtime.compilerservices.unsafe\6.0.0\buildTransitive\netcoreapp2.0\System.Runtime.CompilerServices.Unsafe.targets" Condition="Exists('$(NuGetPackageRoot)system.runtime.compilerservices.unsafe\6.0.0\buildTransitive\netcoreapp2.0\System.Runtime.CompilerServices.Unsafe.targets')" />
  </ImportGroup>
</Project>

文件中没有更多相关差异。

我应该检查/安装什么来修复这个编译错误?

【问题讨论】:

    标签: compiler-errors project nuget-package .net-6.0


    【解决方案1】:

    Visual Studio 2022 的最新更新解决了该问题。 我已经尝试修复 VS2022 并重新安装框架,但这不起作用。

    可能此更新修复了修复无法解决的问题。

    【讨论】:

      猜你喜欢
      • 2022-07-26
      • 2022-06-28
      • 1970-01-01
      • 2015-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 2022-10-15
      • 2014-08-04
      相关资源
      最近更新 更多