当我构建除包含引用的网站之外的任何其他项目时,Visual Studio正在覆盖我在项目引用和NuGet包文件中配置的NewtonSoft.Json.DLL的正确版本.
好.这是场景:
我有一个后端服务和网站的解决方案.该网站在.NET 4.5上运行,并配置了NuGet以引入Newtonsoft.Json.DLL的6.0.1版本.
<package id="Newtonsoft.Json" version="6.0.1" targetFramework="net45" />
Run Code Online (Sandbox Code Playgroud)
这会将dependenAssembly绑定添加到web.config文件中.
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
我可以毫无问题地构建和运行这个网站.
我最近更新了从.NET 4.0到.NET 4.5的所有类库和后端服务.更新后,每当我构建其中一个类库或运行/调试后端服务时,网站就无法运行.
Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)
我跟踪了这一事实,当重建其中一个类库或从Visual Studio运行/调试后端服务时,Newtonsoft.Json.DLL会被旧版本的文件 - 版本4.5.11覆盖.由于显式的dependentAssembly绑定,在我访问网站之后,我得到上面提到的"无法加载..."错误.
如果我只想运行后端服务或网站中的一个或另一个,那就没关系,但我必须同时运行它们才能使我的应用程序正常运行.但由于这个错误,我无法在网站或网站崩溃的同时运行后端服务.
如何防止Visual Studio覆盖DLL?
请注意,我只6.0.1跨整个解决方案的参考设置(即没有参考任何地方到4.5.11).在网站中,我将'Copy Local'设置为true,并将'Specific Version'设置为对Newtonsoft.Json.DLL为true.