Home Unittest project Azure Functions could not load file or assembly 'Newtonsoft.Json'
Reply: 1

Unittest project Azure Functions could not load file or assembly 'Newtonsoft.Json'

Goat
1#
Goat Published in 2017-11-14 14:37:08Z

I created a test project for a Function App as mentioned here. The Http triggered Azure Function I wrote, uses dependency injection (AzureFunctions.AutoFac) and looks like this:

[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);
    }
}

And the unittest class (with Moq, Shouldly and NBuilder):

[TestClass]
public class CreateDeclarationsFunctionTest
{
    private Mock<IDeclarationDataService> _declarationDataService;
    private Mock<IClientDataService> _clientDataService;

    [TestInitialize]
    public void Initialize()
    {
        _declarationDataService = new Mock<IDeclarationDataService>();
        _clientDataService = new Mock<IClientDataService>();
    }

    [TestMethod]
    public void CreateDeclarations_ReturnsOk()
    {
        // Arrange
        var clients = Builder<Client>.CreateListOfSize(10).Build();

        _declarationDataService.Setup(x => x.CreateAndSaveNew(It.IsAny<int>()))
            .Returns(Builder<Declaration>.CreateNew().Build);

        // Act > Exception by calling the Run method
        var result = CreateDeclarationsFunction.Run(CreateRequest(""), new TraceWriterStub(TraceLevel.Info),
            _declarationDataService.Object, _clientDataService.Object);

        // Assert
        // TODO
    }

    private static HttpRequestMessage CreateRequest(string json)
    {
        // Just a mocked request
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri("https://localhost"),
            Content = new StringContent(json, Encoding.UTF8, "application/json")
        };

        return request;
    }
}

When I run this unittest, it gives an exception.

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)

The FunctionApp has 2 nested references to Newtonsoft.Json (9.0.1), but none to version 6.0.0.0.

Dependencies > Nuget > Microsoft.NET``.Sdk.Functions (1.0.6) > Newtonsoft.Json (9.0.1)

Dependencies > Nuget > AzureFunctions.Autofac (2.0.0) > Microsoft.Azure.Webjobs (2.1.0-beta4) > Newtonsoft.Json (9.0.1)

The test project has a reference to the FunctionApp project. The exception is only shown when unit testing and not when its running (and being invoked from the browser). Does anybody have a solution for upper exception? Thanks.

UPDATE

I found out that the System.IO.FileLoadException occurs when returning req.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);

When returning req.CreateResponse(HttpStatusCode.OK); instead, it does not give the mentioned exception.

Goat
2#
Goat Reply to 2017-11-16 09:25:31Z

I think because of the way I setup the test project (initially .NET standard and then converted the target framework manually to .NET Framework 4.6.1 as mentioned here), no app.config file is generated in the test project. Furthermore, I think that such a test project setup does not automatically generate binding redirects for nuget packages when updating the version of a nuget package partly in the solution.

As @FabioCavalcante mentioned, I manually had to add an app.config file to the test project and put a binding redirect for 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>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.339074 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO