Visual Studio 2017 - 无法加载文件或程序集"System.Runtime,Version = 4.1.0.0"或其依赖项之一

Bri*_*ian 89 .net visual-studio-2017 .net-standard-1.5

我正在使用Visual Studio 2017,我正在尝试创建.Net Standard 1.5库并在.Net 4.6.2 nUnit测试项目中使用它.

我收到以下错误...

无法加载文件或程序集'System.Runtime,Version = 4.1.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一.该系统找不到指定的文件.

我尝试过以下方法:

  1. 参考Std库作为项目参考.错误:给我以前的错误.
  2. 为我的Std库创建一个NuGet pkg并引用它.错误:类型是System.String,期望System.String.这是因为System.Runtime最终被项目引用,并且它具有所有标准类型的定义.
  3. 参考NuGet pkg NetStandard.Library.错误:给我与#相同的错误("类型是System.String,期望System.String").注意:在我这样做之前,我清除了项目中的所有NuGet包,然后只添加了nUnit和NetStandard.Library包(安装了45个其他包).

这是一个错误吗?有解决方法吗?任何帮助表示赞赏.

小智 84

我有同样的问题,没有我发现有效的建议解决方案.我对此问题的解决方案是:检查App.config和packages.config以查看版本是否匹配.

最初我的app.config包含:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

但是packages.config包含:

<package id="System.Runtime" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
Run Code Online (Sandbox Code Playgroud)

我修改了app.config条目以匹配newVersion的packages.config:

<dependentAssembly>
  <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.3.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

更改后,问题得到解决.

  • 我从NuGet中取出"4.3.0",但由于某些原因,VS坚持认为我引用了"4.1.2.0",类似的工作只是为我工作的不同版本号...... (6认同)

Cor*_*son 31

当您从.NET 4.x项目引用.NET Standard项目时会发生此问题:.NET Standard项目的nuget包引用都不是作为依赖项引入的.

要解决此问题,您需要确保.NET 4.x csproj文件指向当前构建工具(至少14个):

<Project ToolsVersion="15.0">...
Run Code Online (Sandbox Code Playgroud)

不再需要以下内容,它在VS 15.3附近固定:

VS2017中存在一个已知错误,特别是在NuGet 4.0中.

要解决此问题,您需要打开.NET 4.x项目的.csproj文件并添加以下代码段:

<ItemGroup>
  <PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
    <PrivateAssets>All</PrivateAssets>
  </PackageReference>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud)

NuGet 4.x带来了"软件包引用" - 没有更多的packages.config - 但是在VS2017发布时,旧的4.x管道还没有完全更新.上面的代码片段似乎"唤醒"构建系统以正确地包含依赖项的包引用.

  • 我仍然在15.5.5 VS2017中遇到问题.看起来还有其他原因. (11认同)
  • 版本15.6.4并且解决方案不起作用. (3认同)
  • [此答案](/sf/answers/3435525061/)解决了我的问题。 (2认同)
  • 值得注意的是,Visual Studio 2017版本15.7及更高版本支持将项目从packages.config管理格式迁移到PackageReference格式.https://docs.microsoft.com/en-us/nuget/reference/migrate-packages-config-to-package-reference (2认同)

Tus*_*har 30

我最近遇到了这个问题,我在这个帖子和其他人中尝试过很多东西.我加装基准为"System.Runtime"通过NuGet包管理器,固定在绑定.预测app.config,并确保app.configpackage.config对装配的版本相同.但问题仍然存在.

最后,我删除了<dependentAssembly>程序集的标签,问题消失了.因此,请尝试删除以下内容app.config.

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.1.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

编辑: 在我将.NET框架更新到4.7.2之后,问题重新浮出水面.我尝试了上面的技巧,但它没有用.在浪费了很多时间之后,我意识到问题正在发生,因为System.Linqapp.config中有一个旧的引用.因此,要么删除或更新所有Linq引用也要摆脱这个问题.

  • 每当遇到OP指定的问题时,我都会删除.config文件中的System.Runtime信息,这样可以解决该问题。我同意您的看法,这是一个潜在的有效解决方案。当我从nuget添加软件包时,往往会发生这种情况。 (3认同)

She*_*wal 24

相信我,我不是在开玩笑.从app.config中删除所有System.Runtime依赖项,它将开始工作.

  • 更好地解释为什么这会起作用会有所帮助. (9认同)

Nof*_*fls 13

我通过引用NUnit-Project中的NetStandard.Library和以下app.config文件来解决该错误.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.1.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)

编辑

如果有什么事情比其他的System.Runtime,System.Reflection或者System.Runtime.InteropServices已经丢失(例如System.Linq),然后只需添加一个新的dependentAssembly节点.

编辑2

在新的Visual Studio版本(2017 15.8我认为)中,Studio可能会创建app.config文件.只需检查项目属性 - 应用程序中自动生成绑定重定向复选框. 自动生成绑定重定向

  • 奇怪的是,我通过_removing_为System.Runtime的所有`<dependentAssembly>`节点为我解决了这个问题. (10认同)

小智 13

我通过删除我app.config来修复它

<assemblyIdentity name="System.Runtime" ....> 
Run Code Online (Sandbox Code Playgroud)

条目.

app.config 在重构期间自动添加(但不需要)


Mic*_*tot 5

进入 app.config 或 web.config 添加

<dependentAssembly>
    <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)