Azure / Azure/azure-functions-host

Could not load file or assembly 'System.Net.Http or one of its dependencies while unit testing Azure Function Project

Open
#1,922 3 comments 0 reactions 1 assignee Claimed by @vijayrkn View on GitHub
bug
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 10h
Merged PRs (30d)
36

Description

While unit testing Azure Functions Project i get this this exception running my test case:

> Could not load file or assembly 'System.Net.Http, Version=4.1.10, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ or one of its dependencies.

#### Investigative information
I'm using Visual Studio 2017 15.3.4

#### Repro steps
Step 1:
Create Azure Functions Project for HttpTrigger using VS template (it's targetted at .net framework 4.6.1)

Step 2:
Then create a unit test project (4.6.1 framework). Add reference to the azure function projected created above.

Step 3:
Run the sample test case
You get the exception mentioned above.

Provide the steps required to reproduce the problem:

#### Expected behavior

Expected behavior is that the test cases runs successfully without complaining about this issue with System.Net.Http dll

#### Known workarounds

Please check the response from David on this stackoverflow issue that i created. this workaround worked for me.

https://stackoverflow.com/questions/46287628/could-not-load-file-or-assembly-system-net-http-version-4-1-1-0-in-unit-test-fo

In short, i had to create the unit test project using .net core template, edit the csproj file to change the target framework to net461, then everything worked fine.

#### Related information
Here is the code for unit test that validates the response from azure function

```csharp
public async Task TestMethod1()
{
//Arrange
var configuration = new HttpConfiguration();
var req = new HttpRequestMessage()
{
Content = new StringContent(string.Empty),
RequestUri = new Uri("http://localhost?name=test"),
Method = HttpMethod.Get,
};

req.Properties[System.Web.Http.Hosting.HttpPropertyKeys.HttpConfigurationKey] = configuration;
TraceWriter log = new TraceMonitor();

//Call azure function
var result = await TestHttpFunction.Run(req, log).ConfigureAwait(false);
var content = await result.Content.ReadAsStringAsync().ConfigureAwait(false);
Assert.IsTrue(result.IsSuccessStatusCode);
Assert.AreEqual(content, "\"Hello Test\"");
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.