C# Newtonsoft.Json问题:无法从源System.Net.Http.Formatting加载文件或程序集异常
我在.NET4.5中完成了Windows服务。它引用了另一个VisualStudio解决方案中的DLL,例如myCustomDLL。myCustomDLL引用了Newtonsoft.Json DLL版本11.0.1,还引用了System.Net.Http.Formatting版本5.2.6.0 当我从Visual Studio调试窗口服务并调用myCustomDLL中的函数时,我得到错误:C# Newtonsoft.Json问题:无法从源System.Net.Http.Formatting加载文件或程序集异常,c#,visual-studio-2013,json.net,.net-4.5,C#,Visual Studio 2013,Json.net,.net 4.5,我在.NET4.5中完成了Windows服务。它引用了另一个VisualStudio解决方案中的DLL,例如myCustomDLL。myCustomDLL引用了Newtonsoft.Json DLL版本11.0.1,还引用了System.Net.Http.Formatting版本5.2.6.0 当我从Visual Studio调试窗口服务并调用myCustomDLL中的函数时,我得到错误: Could not load fiile or assembly 'Newtonsoft.Json, Ve
Could not load fiile or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies.
Source: System.Net.Http.Formatting
StackTrace:
at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor()
at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content)
at My.Namespace.MyAPI.Send(TaskData data) en d:\MyProjects\Dev\My.Custom.Namespace\myApi.cs:line 80
我没有在myCustomDLL上安装任何Newtonsoft.json DLL版本4.5.0.0
另外,我的WindowsSercice(在vb.net中完成)添加了一个指向Newtonsoft.Json 11.0.1版的引用
解决方案:
最后,我完成了@Richard在评论中的建议,nilsK回答了。
Windows服务已添加对正确的Newtonsoft.Json(11.0.1)的引用,但Windows服务配置文件(app.config)中缺少汇编绑定行,因此我添加了这些行,并成功:
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
在这里写作,因为更好的格式。。。我只想补充一下@Richard的评论 在app.config中,您将看到如下所示的一些行(可能看起来有点不同):
将该版本号更改为您需要的版本,即“11.0.1”(或“12.0.3”,当前稳定版本)。在此处写入,因为更好的格式。。。我只想补充一下@Richard的评论 在app.config中,您将看到如下所示的一些行(可能看起来有点不同):
将该版本号更改为您需要的版本,即“11.0.1”(或“12.0.3”,当前稳定版本)。如果
System.IO.FileLoadException
即使在绑定重定向添加到Web.config后仍继续抛出,则绑定运行时/assemblyBinding
配置块中可能存在语法错误
例如,由于错误的合并,我在同一个
块中有两个
块。这导致所有的
无效,导致在运行时找不到DLL。我可以想象其他微妙的语法错误也会有同样的副作用
<runtime>
<assemblyBinding>
...
<!-- Problematic dependentAssembly line -->
<dependentAssembly>
<assemblyIdentity name="PackageA" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
<!-- Example of syntax error -->
<!-- Two sets of bindingRedirects should NOT be placed in the same dependentAssembly -->
<assemblyIdentity name="PackageB" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
<dependentAssembly/>
</assemblyBinding>
</runtime>
...
如果即使在将绑定重定向添加到Web.config后仍继续抛出System.IO.FileLoadException
,则绑定运行时/assemblyBinding
配置块中可能存在语法错误
例如,由于错误的合并,我在同一个
块中有两个
块。这导致所有的
无效,导致在运行时找不到DLL。我可以想象其他微妙的语法错误也会有同样的副作用
<runtime>
<assemblyBinding>
...
<!-- Problematic dependentAssembly line -->
<dependentAssembly>
<assemblyIdentity name="PackageA" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
<!-- Example of syntax error -->
<!-- Two sets of bindingRedirects should NOT be placed in the same dependentAssembly -->
<assemblyIdentity name="PackageB" publicKeyToken="token" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
<dependentAssembly/>
</assemblyBinding>
</runtime>
...
Quick answer,您的Windows服务项目的配置可能需要一个assemblyBinding
条目,以确保加载了正确版本的Newtonsoft.Json。@Richard抱歉,我忘了说我的Windows服务添加了对Newtonsoft.Json 11.0.1版的引用。我将更新帖子。@Richard谢谢,它成功了!快速回答,您的Windows服务项目的配置可能需要一个assemblyBinding
条目,以确保加载了正确版本的Newtonsoft.Json。@Richard抱歉,我忘了说我的Windows服务添加了对Newtonsoft.Json 11.0.1版的引用。我将更新帖子。@Richard谢谢,它成功了!是的,我终于按照@Richard的建议做了,效果不错。我加了一个装订。它不在我的windows服务的配置文件中。它只添加了对Newtonsoft.Json的引用。windows服务配置文件中的AssemblyBinding丢失。是的,我终于按照@Richard的建议完成了,并且工作正常。我加了一个装订。它不在我的windows服务的配置文件中。它只添加了对Newtonsoft.Json的引用。缺少windows服务配置文件中的AssemblyBinding。