我遇到了同样的问题,但找到的建议解决方法都没有用。我解决这个问题的方法是:检查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>
但是packages.config文件包含了以下内容:
<package id="System.Runtime" version="4.3.0" targetFramework="net461" requireReinstallation="true" />
我修改了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>
更改后,问题得到解决。
最近我遇到了这个问题,尝试了在这个线程和其他线程中提到的很多方法。通过NuGet包管理器添加了“System.Runtime”包引用,修复了app.config
中的绑定重定向,并确保app.config
和package.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>
编辑:
在我更新.NET框架到4.7.2之后,问题重新出现。我尝试了上面的技巧,但它没有起作用。浪费了很多时间后,我意识到问题是由于app.config中旧的System.Linq
引用引起的。因此,要想摆脱这个问题,要么移除或更新所有Linq引用。
xunit System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=4.1.2.0
。 - Anton Krouglov<Project ToolsVersion="15.0">...
以下内容不再需要,已在VS 15.3左右修复:
在VS2017中存在一个已知的错误,特别是在NuGet 4.0中。
为了解决这个问题,您需要打开 .NET 4.x 项目的 .csproj 文件,并添加以下代码片段:
<ItemGroup>
<PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
System.Runtime
引用。 - skolima相信我,我不是开玩笑。 从您的app.config中删除所有System.Runtime依赖项,它将开始工作。
我通过引用NetStandard.Library和在NUnit项目中使用下列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>
编辑
如果除System.Runtime
、System.Reflection
或System.Runtime.InteropServices
之外的任何内容都丢失了(例如System.Linq
),那么只需添加一个新的dependentAssembly
节点。
编辑2
在新的Visual Studio版本(我认为是2017 15.8)中,Studio可能会创建app.config文件。只需检查项目属性-应用程序中的自动生成绑定重定向复选框。
编辑3
自动生成绑定重定向在.NET Classlibrary中效果不佳。 将以下行添加到csproj文件中,可以解决这个问题,并生成一个适用于Classlibary的工作.config文件。
<PropertyGroup>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>
<dependentAssembly>
节点来解决了我的问题。 - Matt Brewerton我通过删除我的app.config
文件来解决了它。
<assemblyIdentity name="System.Runtime" ....>
条目。
app.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>
这个问题有很多原因......在我的情况下,问题是在我的 web.config
文件中加了一个标签来添加 System.Runtime
组件:
<assemblies>
<add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</assemblies>
<package id="System.Runtime" version="4.3.0" targetFramework="net47" />
从我的web.config
中删除<add assembly>
标签解决了这个问题。
[MSTest][Discovery] 无法从程序集中发现测试 原因:无法加载文件或程序集'System.Reflection, Version=4.1.1.0等
- Dan M