C# Unittest project Azure函数无法加载文件或程序集';Newtonsoft.Json';
如前所述,我为一个函数应用程序创建了一个测试项目。我编写的Http触发Azure函数使用依赖项注入(),如下所示:C# Unittest project Azure函数无法加载文件或程序集';Newtonsoft.Json';,c#,.net,unit-testing,azure,azure-functions,C#,.net,Unit Testing,Azure,Azure Functions,如前所述,我为一个函数应用程序创建了一个测试项目。我编写的Http触发Azure函数使用依赖项注入(),如下所示: [DependencyInjectionConfig(typeof(DependencyConfig))] public static class CreateDeclarationsFunction { [FunctionName("CreateDeclarationsFunction")] public static HttpResponseMessage Ru
[DependencyInjectionConfig(typeof(DependencyConfig))]
public static class CreateDeclarationsFunction
{
[FunctionName("CreateDeclarationsFunction")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "CreateDeclarations")]HttpRequestMessage req,
TraceWriter log,
[Inject]IDeclarationDataService declarationDataService,
[Inject]IClientDataService clientDataService)
{
log.Info("Http triggered function: CreateDeclarationsFunction processed a request.");
try
{
var clients = clientDataService.GetAll().ToList();
foreach (var client in clients)
{
// Create and save new declaration for each client
declarationDataService.CreateAndSaveNew(client.Id);
}
}
catch (Exception ex)
{
return req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}
return req.CreateResponse(HttpStatusCode.OK);
}
}
以及unittest类(含Moq、Shouldly和NBuilder):
functionPP
有两个嵌套引用到Newtonsoft.Json(9.0.1)
,但没有一个嵌套引用到版本6.0.0
依赖项
Nuget
Microsoft.NET``.Sdk.Functions(1.0.6)
Newtonsoft.Json(9.0.1)
Dependencies
NugetAzureFunctions.Autofac(2.0.0)
Microsoft.Azure.Webjobs(2.1.0-beta4)
Newtonsoft.Json(9.0.1)
测试项目引用了FunctionApp
项目。该异常仅在单元测试时显示,而在其运行(以及从浏览器调用)时不显示。
有人能解决上面的异常吗?谢谢
更新
我发现返回req.CreateErrorResponse(HttpStatusCode.InternalServerError,ex)时发生System.IO.FileLoadException
代码>
返回req.CreateResponse(HttpStatusCode.OK)时代码>相反,它没有给出所提到的异常 我认为,由于我设置测试项目的方式(最初是.NET standard
,然后手动将目标框架转换为.NET framework 4.6.1
),测试项目中不会生成app.config
文件。此外,我认为这样的测试项目设置在更新部分解决方案中的nuget包版本时不会自动为nuget包生成绑定重定向
Result StackTrace:
at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
at System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config)
at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Func`2 errorCreator)
at System.Net.Http.HttpRequestMessageExtensions.CreateErrorResponse(HttpRequestMessage request, HttpStatusCode statusCode, Exception exception)
at FunctionApp.CreateDeclarationsFunction.Run(HttpRequestMessage req, TraceWriter log, IDeclarationDataService declarationDataService, IClientDataService clientDataService)
at FunctionApp.Tests.CreateDeclarationsFunctionTest.CreateDeclarations_ReturnsOk() in C:\TFS\...\FunctionApp.Tests\CreateDeclarationsFunctionTest.cs:line 63
Result Message:
Test method FunctionApp.Tests.CreateDeclarationsFunctionTest.CreateDeclarations_ReturnsOk threw exception:
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
如前所述,我必须手动向测试项目添加一个app.config
文件,并为Newtonsoft.Json
设置绑定重定向
<?xml version="1.0" encoding="utf-8" ?>
<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-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
您的测试项目中是否有一个app.config文件,其中包含Newtonsoft.Json的绑定重定向?可能存在一个正确的绑定重定向(类似于此:)help@FabioCavalcante事实上,这就是答案。非常感谢。
<?xml version="1.0" encoding="utf-8" ?>
<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-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>