无法在 Azure Functions 的单元测试中加载文件或程序集 System.Net.Http,版本 = 4.1.1.0 [英] Could not load file or assembly System.Net.Http, Version=4.1.1.0 in Unit Test for Azure Functions

查看:17
本文介绍了无法在 Azure Functions 的单元测试中加载文件或程序集 System.Net.Http,版本 = 4.1.1.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Azure Function 项目(使用 VS 2017 15.3.4 创建).我在同一个解决方案(4.6.1)中添加了一个单元测试项目.当我添加对 Azure 函数项目的引用并尝试运行我的简单测试用例时,我在执行 MyFunction 的 Run 方法时收到此错误无法加载文件或程序集 System.Net.Http,版本 = 4.1.1.0".

异常图片我已经尝试了很多推荐的方法,但似乎没有任何效果.

我尝试在 app.config 文件中添加绑定

<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>

我已尝试显式添加 System.Net.http 的 nuget 包(从最新版本到后面的几个版本),但这也无济于事.

有没有人设法找到解决方案?

谢谢桑杰

解决方案

有一种方法可以使这个工作,但并不完全简单:

  • 创建单元测试 (.NET Core) 项目,而不是单元测试 (.NET Framework).这会强制它使用新的项目系统.
  • 编辑单元测试项目的 csproj.
  • <TargetFramework>netcoreapp2.0 更改为 net461.注意:您无法从 VS UI 更改此设置,因此需要手动编辑!
  • 在 VS 中,添加对 Functions 项目的引用

现在一切都会好很多了.例如有关完整示例,请参阅 https://github.com/vijayrkn-test/FunctionsProjectWithClassLib.p>

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

解决方案

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.

这篇关于无法在 Azure Functions 的单元测试中加载文件或程序集 System.Net.Http,版本 = 4.1.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆