C# 错误单元测试webapi控制器
我正在使用ASPNetWebAPI客户端5.0,并尝试对WebAPI控制器进行单元测试C# 错误单元测试webapi控制器,c#,asp.net,unit-testing,asp.net-web-api,json.net,C#,Asp.net,Unit Testing,Asp.net Web Api,Json.net,我正在使用ASPNetWebAPI客户端5.0,并尝试对WebAPI控制器进行单元测试 var encservice = new EncryptionService(); var acctservice = FakeServices.GetAccountService(); var controller = new AccountController(acctservice, encservice); controller.Request = new HttpRequestMessage();
var encservice = new EncryptionService();
var acctservice = FakeServices.GetAccountService();
var controller = new AccountController(acctservice, encservice);
controller.Request = new HttpRequestMessage();
当代码
controller.Request.SetConfiguration(new HttpConfiguration());
当我遇到异常时执行
消息:无法加载文件或程序集“Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6eed”或其依赖项之一。定位的程序集清单定义与程序集引用不匹配。(来自HRESULT的异常:0x8013100)
来源:System.Net.Http.Formatting
Stacktrace:在System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()上
在System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()中
在System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor()中
在System.Web.Http.HttpConfiguration.DefaultFormatters()中
位于System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection路由)
在System.Web.Http.HttpConfiguration..ctor()上
在c:\PremiumProjectsCollection\EMR\src\EMRAzure\EMRAzure\EMR.Test\Controller.AccountControllerTest.中的EMR.Test.Controller.AccountControllerTest.应该\u Get():第34行
我使用的newsoft.json版本是6.0
我的配置文件中还有一个程序集重定向
<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>
im使用的测试运行程序是MStest,VS2012,您需要添加一个:
(假设Newtonsoft.Json的汇编版本正好是6.0.0.0。)我自己没有尝试过,但2012 mstest中似乎有一个bug。您必须使用
.testsettings
文件才能获取app.config
请参阅以下链接:(注意,下面的注释是指存在此问题的Web Api客户端项目)
我对Newtonsoft.Json的版本也有同样的问题,所以我删除了旧版本的引用,并使用PackageManager控制台在我的Web Api客户端库和测试项目上安装了最新版本的Newtonsoft.Json
安装软件包Newtonsoft.Json-版本6.0.8
(注意:您可能需要找出哪个是最新版本)
问题仍然存在,因此我意识到System.Net.Http.Formatting和我最新版本的Json之间发生了崩溃。
要解决此问题,请删除System.Net.Http和System.Net.Http.FORMATING引用,改为通过Nuget安装WebApi客户端库,如下所示:
安装软件包Microsoft.AspNet.WebApi.Client
这为我解决了问题 我几天前就遇到了同样的问题,我花了几个小时才找到解决这个问题的办法 我正在测试一个安装了最新版本NewtonSoft的单元,而我的测试项目安装了较旧的版本 我所做的是通过在解决方案资源管理器中右键单击解决方案,通过“Manage Nuget packages for solution”选项将此库的版本整合到我的解决方案中
这将更新当前解决方案下项目中存在的所有NewtonSoft库,并从VisualStudio在解决方案目录中名为packages的文件夹中创建的包控件中删除所有旧版本。谢谢标记,但我的app.config中已经有了它。它仍然会出现异常。哪个app.config文件?是的,我的app.config中有它。在使用mstest+vs2012IME的单元测试项目am中,您发布的异常消息几乎总是由不同的程序集版本引起的。修复程序始终是程序集重定向。但是,只有当
publicKeyToken
在各个版本中保持不变时,这才有效。你有没有检查过情况是否如此?IIRC,Newtonsoft.Json更改公钥令牌可能有一些问题,但我可能记错了…我面临着同样的问题。你解决过吗?@Jean-Françoisbauchamp,我最终回到了Newtonsoft.Json 4.5.0.0,我遇到了同样的问题。你解决过这个问题吗?@ChuckConway不,我最终还是回到了Newtonsoft.Json 4.5.0.0
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<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>
</assemblyBinding>
</runtime>
</configuration>