Failed to load file or assembly System.Net.Http, Version = 4.1.1.0 in Unit Test for Azure functions

I have an Azure Function project (created using VS 2017 15.3.4). I added the unit test project in the same solution (4.6.1). As soon as I add a link to my Azure function project, and I try to run my simple test case, I get this error "Failed to load the file or assembly System.Net.Http, Version = 4.1.1.0" when the MyFunction Run method starts.

Exceptional image I tried many recommended ones, but nothing works.

I tried adding a binding to the app.config file

<dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" /> </dependentAssembly> 

I tried to add the nuget package for System.Net.http explicitly (from the latest version to several versions behind), but this does not help.

Has anyone been able to find a solution for this?

Thanks Sanjay

+1
source share
1 answer

There is a way to make this work, but it is not quite simple:

  • Create a Unit Test (.NET Core) project, not a Unit Test (.NET Framework) . This forces him to use the new Project system.
  • Edit the csproj project for the unit test project.
  • Change <TargetFramework> from netcoreapp2.0 to net461 . Note: you cannot change this from the VS user interface, hence manual editing!
  • In VS add a link to your function project

And now everything should work much better. for example see https://github.com/vijayrkn-test/FunctionsProjectWithClassLib for a complete sample.

+4
source

Source: https://habr.com/ru/post/1015347/


All Articles