Could not load file or assembly System.Net.Http, V

2019-02-25 17:53发布

问题:

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

Exception Image I have tried alot of approached recommended but nothing seems to work.

I have tried to add the the binding in 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 have tried to add the nuget package for System.Net.http explicitly (from latest version to few version behind) but that's not helping either.

has anyone managed to find the solution for this?

Thanks Sanjay

回答1:

There is a way to make this work, but it is not completely straightforward:

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

And now everything should work a lot better. e.g. see https://github.com/vijayrkn-test/FunctionsProjectWithClassLib for a full sample.