.net Visual Studio 2017 - 无法加载文件或程序集“System.Runtime,Version=4.1.0.0”或其依赖项之一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42755274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies
提问by Brian
I am using Visual Studio 2017 and am trying to create a .Net Standard 1.5 library and use it in a .Net 4.6.2 nUnit test project.
我正在使用 Visual Studio 2017 并尝试创建一个 .Net Standard 1.5 库并在 .Net 4.6.2 nUnit 测试项目中使用它。
I am getting the following error...
我收到以下错误...
Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
无法加载文件或程序集“System.Runtime,Version=4.1.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。该系统找不到指定的文件。
I have tried the following:
我尝试了以下方法:
- Reference Std library as project reference.Error: gives me the previous error.
- Create a NuGet pkg for my Std library and reference that.Error: The type is System.String, expecting System.String. This is because System.Runtime ended up getting referenced by the project and it has definitions for all the standard types.
- Reference NuGet pkg NetStandard.Library.Error: give me the same error as # ("The type is System.String, expecting System.String"). NOTE: Before I did this, I cleared ALL NuGet packages from the project and then added just the nUnit and NetStandard.Library packages (which installed 45 other packages).
- 参考标准库作为项目参考。错误:给我以前的错误。
- 为我的标准库创建一个 NuGet pkg 并引用它。错误:类型是 System.String,需要 System.String。这是因为 System.Runtime 最终被项目引用,并且它具有所有标准类型的定义。
- 参考 NuGet pkg NetStandard.Library。错误:给我与 # 相同的错误(“类型是 System.String,期望 System.String”)。注意:在我这样做之前,我从项目中清除了所有 NuGet 包,然后只添加了 nUnit 和 NetStandard.Library 包(安装了 45 个其他包)。
Is this a bug? Is there a work-around? Any help is appreciated.
这是一个错误吗?有解决办法吗?任何帮助表示赞赏。
回答by Ty Petrice
I had the same problem and no suggested solutions that I found worked. My solution for this issue was: Check App.config and packages.config to see if the versions match.
我遇到了同样的问题,但没有发现可行的建议解决方案。我对此问题的解决方案是:检查 App.config 和 packages.config 以查看版本是否匹配。
Originally my app.config contained:
最初我的 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>
But the packages.config contained:
但是packages.config 包含:
<package id="System.Runtime" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
I modified the app.config entry to match packages.config for the newVersion:
我修改了 app.config 条目以匹配新版本的 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>
After the change, the issue was resolved.
更改后,问题得到解决。
回答by Cory Nelson
This issue happens when you reference a .NET Standard project from a .NET 4.x project: none of the .NET Standard project's nuget package references are brought in as dependencies.
当您从 .NET 4.x 项目引用 .NET Standard 项目时会发生此问题:没有任何 .NET Standard 项目的 nuget 包引用作为依赖项引入。
To fix this, you need to ensure your .NET 4.x csproj file is pointing to current build tools (at least 14):
要解决此问题,您需要确保 .NET 4.x csproj 文件指向当前的构建工具(至少 14 个):
<Project ToolsVersion="15.0">...
The below should no longer be needed, it was fixed around VS 15.3:
不再需要以下内容,它已在 VS 15.3 左右修复:
There was a known bugin VS2017, specifically in NuGet 4.0.
VS2017 中存在一个已知错误,特别是在 NuGet 4.0 中。
To work around the bug, you'll need to open up the .csproj file for your .NET 4.x project and add this snippet:
要解决此错误,您需要打开 .NET 4.x 项目的 .csproj 文件并添加以下代码段:
<ItemGroup>
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
NuGet 4.x brings with it the "package reference" -- no more packages.config -- but the old 4.x pipeline was not fully updated at the time of VS2017's launch. The above snippet seems to "wake up" the build system to correctly include package references from dependencies.
NuGet 4.x 带来了“包参考”——不再是 packages.config——但是旧的 4.x 管道在 VS2017 发布时还没有完全更新。上面的代码片段似乎“唤醒”了构建系统以正确包含依赖项中的包引用。
回答by Tushar
I encounter this issue recently and I tried many things mentioned in this thread and others. I added package reference for "System.Runtime"by nuget package manager, fixed the binding redicts in app.config, and make sure that app.configand package.confighave the same version for the assembly. However, the problem persisted.
我最近遇到了这个问题,我尝试了这个线程和其他线程中提到的很多东西。我加装基准为"System.Runtime"通过NuGet包管理器,固定在结合。预测app.config,并确保app.config和package.config对装配的版本相同。然而,问题仍然存在。
Finally, I removed the <dependentAssembly>tag for the assembly and the problem dissappeared. So, try removing the following in your app.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>
Edit:After I update .NET framework to 4.7.2, the problem resurfaced. I tried the above trick but it didn't work. After wasting many hours, I realized the problem is occurring because of an old System.Linqreference in app.config. Therefore, either remove or update all Linq references also to get rid of this problem.
编辑:在我将 .NET 框架更新到 4.7.2 后,问题又出现了。我尝试了上述技巧,但没有奏效。浪费了几个小时后,我意识到问题是由于System.Linqapp.config中的旧参考而发生的。因此,删除或更新所有 Linq 引用也可以解决此问题。
回答by Sheena Agrawal
Trust me, I am not joking. Remove all the System.Runtime dependencies from your app.config and it will start working.
相信我,我不是在开玩笑。从 app.config 中删除所有 System.Runtime 依赖项,它将开始工作。
回答by Noffls
I resolved that error by referencing the NetStandard.Libraryand the following app.configFile in the NUnit-Project.
我通过引用NUnit 项目中的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>
Edit
编辑
If anything other than System.Runtime, System.Reflectionor System.Runtime.InteropServicesis missing (e.g. System.Linq), then just add a new dependentAssemblynode.
如果System.Runtime,System.Reflection或之外的任何内容System.Runtime.InteropServices丢失(例如System.Linq),则只需添加一个新dependentAssembly节点。
Edit 2
编辑 2
In new Visual Studio Versions (2017 15.8 I think) it's possible that Studio creates the app.config File. Just check the Auto-generate binding redirectsCheckbox in Project-Properties - Application.

在新的 Visual Studio 版本(我认为是 2017 15.8)中,Studio 可能会创建 app.config 文件。只需选中Project-Properties - Application 中的Auto-generate binding redirectsCheckbox 。

Edit 3
编辑 3
Auto-generate binding redirectsdoes not work well with .NET Classlibraries. Adding the following lines to the csproj files solves this and a working .config file for the Classlibary will be generated.
自动生成绑定重定向不适用于 .NET 类库。将以下行添加到 csproj 文件可以解决这个问题,并将生成 Classlibary 的工作 .config 文件。
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
回答by Marco
I fixed it by deleting my app.configwith
我通过删除我app.config的
<assemblyIdentity name="System.Runtime" ....>
entries.
条目。
app.configwas automatically added (but not needed) during refactoring
app.config在重构期间自动添加(但不需要)
回答by shine
This issue happens when you reference a .NET Standard project from a .NET 4.x project: none of the .NET Standard project's nuget package references are brought in as dependencies.
当您从 .NET 4.x 项目引用 .NET Standard 项目时会发生此问题:没有任何 .NET Standard 项目的 nuget 包引用作为依赖项引入。
I resolved by add System.Runtime 4.3and NETStandard.Library package and !!important!! I use refactor tool to look up the System.Runtime.dll version, It is 4.1.1.1not 4.3and then add an bindingRedirect in .config
我通过添加 System.Runtime4.3和 NETStandard.Library 包和!!important解决了这个问题!我使用重构工具查找 System.Runtime.dll 版本,它4.1.1.1不是4.3然后在 .config 中添加一个 bindingRedirect
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="4.1.1.1" />
</dependentAssembly>
回答by Mesut erdo?an
it is too late I know, howewer there is no succesfully answer. I found the answer from another website. I fixed the issue when I delete the System.Runtime assemblydependency. I deleted this.
我知道为时已晚,但是没有成功的答案。我从另一个网站找到了答案。我在删除 System.Runtime assemblydependency 时解决了这个问题。我删除了这个。
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0"/>
</dependentAssembly>
Best Regards
此致
回答by psychoboi111
Seems like the issue is caused when there is version conflict between packages.config and app.config. In app.config you have assembly binding redirects automatically generated by thing called "AutoGenerateBindingRedirects". When enabled each time you download nuget package it will, additionaly to making new entry in packages.config, add this binding redirect information to app.config, what's the purpose of this is explained here: Assembly Binding redirect: How and Why?
问题似乎是由于packages.config 和app.config 之间存在版本冲突引起的。在 app.config 中,您有由名为“AutoGenerateBindingRedirects”的东西自动生成的程序集绑定重定向。当您每次下载 nuget 包时启用它,除了在 packages.config 中创建新条目之外,还将此绑定重定向信息添加到 app.config,这里解释了这样做的目的是什么:Assembly Binding redirect: How and Why?
There you can read what user @Evk wrote:
在那里您可以阅读用户@Evk 所写的内容:
Why are binding redirects needed at all? Suppose you have application A that references library B, and also library C of version 1.1.2.5. Library B in turn also references library C, but of version 1.1.1.0. Now we have a conflict, because you cannot load different versions of the same assembly at runtime. To resolve this conflict you might use binding redirect, usually to the new version
为什么根本需要绑定重定向?假设您有引用库 B 的应用程序 A 和版本 1.1.2.5 的库 C。库 B 反过来也引用了库 C,但版本为 1.1.1.0。现在我们有一个冲突,因为你不能在运行时加载同一个程序集的不同版本。要解决此冲突,您可以使用绑定重定向,通常到新版本
So, QUICK FIX:Remove all entries in app.config.
因此, 快速修复:删除 app.config 中的所有条目。
In my case just by doing that program started working, but it will probably work only if you don't have any version conflicts of the same assembly at runtime.
在我的情况下,只是通过执行该程序开始工作,但只有在运行时没有任何相同程序集的版本冲突时,它才可能工作。
If you do have such conflict you should fix these version numbers in app.config to match actually used versions of assemblies, but manual process is painful, so I suggest to auto-generate them again by opening Package Manager Console and perform packages reinstallation by typingUpdate-Package -reinstall
如果你确实有这样的冲突,你应该在 app.config 中修复这些版本号以匹配实际使用的程序集版本,但是手动过程很痛苦,所以我建议通过打开包管理器控制台再次自动生成它们并通过键入执行包重新安装Update-Package -reinstall
回答by Silas Humberto Souza
This issue has many causes... in my case the problem was that was in my web.config a tag adding the System.Runtime assembly:
这个问题有很多原因......在我的情况下,问题是在我的 web.config 中添加了 System.Runtime 程序集的标签:
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
but one package also added the same assembly as dependency with other version:
但是一个包还添加了与其他版本相同的程序集作为依赖项:
<package id="System.Runtime" version="4.3.0" targetFramework="net47" />
removing the "add assembly" tag from my web.config resolved the issue.
从我的 web.config 中删除“添加程序集”标签解决了这个问题。

