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

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

问题描述

我有一个Azure Function项目(使用VS 2017 15.3.4创建).我在同一解决方案(4.6.1)中添加了一个单元测试项目.当我添加对Azure函数项目的引用,并尝试运行我的简单测试用例时,当我执行MyFunction的Run方法时,出现此错误无法加载文件或程序集System.Net.Http,Version = 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 . >

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功能的单元测试中加载文件或程序集System.Net.Http,Version = 4.1.1.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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