在运行时在Azure Function中找不到HttpRequestMessageExtensions [英] HttpRequestMessageExtensions not being found at run-time in Azure Function

查看:80
本文介绍了在运行时在Azure Function中找不到HttpRequestMessageExtensions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Azure Function应用程序,该应用程序创建一个预编译的DLL(因此它使用普通的.cs文件,而不是较旧的.csx方法,VS2017之前).以前,它的目标是.Net Framework 4.5.2.我将其更新为4.7,以便使用某些新的C#7功能.我通过执行"Update-Package -Reinstall"更新了NuGet软件包,并验证了它们在我的packages.config文件中都设置了"net47"目标.

I've got an Azure Function app that creates a precompiled DLL (so it uses normal .cs files, not the older .csx method, pre-VS2017). Previously, it was targeting .Net Framework 4.5.2. I updated it to 4.7 so as to use some of the new C# 7 features. I updated my NuGet packages by doing "Update-Package -Reinstall" and verified that they all have the "net47" target set in my packages.config file.

一切都可以编译.但是,当我调用使用2个HttpRequestMessageExtensions方法之一的函数时,出现异常.例外的一个例子是:

Everything compiles fine. But when I call a function that uses either of 2 HttpRequestMessageExtensions methods, I get an exception. One example of the exception is this:

Method not found: 'System.Net.Http.HttpResponseMessage 
  System.Net.Http.HttpRequestMessageExtensions.CreateResponse(
  System.Net.Http.HttpRequestMessage, System.Net.HttpStatusCode)'.

这是一个会导致错误的微型测试函数的示例:

Here's an example of a tiny test function that will cause the error:

using System.Net;
using System.Net.Http;
public static HttpResponseMessage Run(HttpRequestMessage req)
{
        return req.CreateResponse(HttpStatusCode.Accepted, "");         
}       

用Postman调用此函数后,我将收到上述异常.当我在HttpRequestMessage上调用GetQueryNameValuePairs()时,也会得到类似的方法未找到异常.

Upon calling this function with say Postman, I'll receive the aforementioned exception. I also get a similar method not found exception when I call GetQueryNameValuePairs() on the HttpRequestMessage.

我尝试将NuGet软件包更新为最新版本,没有区别.我已经清理,重建并重新启动了很多次,请确保核对bin和obj目录.

I've tried updating my NuGet packages to the latest, no difference. I've cleaned and rebuilt and restarted a bunch of times, making sure to nuke my bin and obj directories.

我不确定这可能是什么问题.我想我可以降级到.Net 4.5.2,但我宁愿不降级.对于一个,我想使用C#7,对于两个,我想了解问题所在,而不是避免它.

I'm not sure what could be the problem. I guess I could downgrade back to .Net 4.5.2 but I'd rather not. For one, I want to use C# 7, and for two, I want to understand what the problem is rather than avoid it.

更新:有趣.问题似乎与System.Net.Http有关.如果我将其降低到4.0.0,一切正常.如果将其升级到任何更高的版本,都会遇到上面列出的问题.我试图有选择地将我的每一个软件包一个一个地降低到其先前的版本号,以找出答案.然后,我将除此以外的所有内容都更新为最新版本,并解决了该问题.

Update: interesting. The issue seems to be with System.Net.Http. If I lower it to 4.0.0 everything works fine. If I raise it to any higher version I get the issues listed above. I tried selectively lowering each of my packages, one by one, to their previous version number to find this out. I then updated all but this one to the latest version and it fixed the issue.

推荐答案

我也对它进行了测试.该问题与System.Net.Http程序集的最新版本(4.3.2)有关.如果我不手动安装此软件包或不安装早期版本(4.3.1/4.3.0),则该应用程序可以正常工作.

I also tested it on my side. The issue is related to the latest version of System.Net.Http assembly(4.3.2). If I don't install this package manually or install the earlier versions(4.3.1/4.3.0), the application could work fine.

CreateResponse方法是在System.Web.Http程序集(版本5.2.3)中编写的扩展方法.似乎它与最新版本的System.Net.Http不兼容.请使用较早版本的System.Net.Http跳过此错误,也可以使用关注频道将此问题提交给Microsoft.

The CreateResponse method is a extension method which is written in System.Web.Http assembly(version 5.2.3). It seem that it is not compatible with the latest version of System.Net.Http. Please could just skip the error by using the earlier version of System.Net.Http and you can also submit this issue to Microsoft using follow channel.

https://connect.microsoft.com/VisualStudio/Feedback

有趣.对我来说,如果我的版本高于4.0.0(包括4.1.1或4.3.1),我仍然会遇到相同的问题,即找不到这些扩展方法.

Interesting. For me, if I got above version 4.0.0 (including 4.1.1 or 4.3.1) I still get the same problem of not finding those extension methods.

在更改程序包版本期间,可能不会更新程序集.从bin \ Debug \ net47文件夹中,我们可以检查所使用的当前程序集版本.

The assembly might not be updated during you change the package version. From the bin\Debug\net47 folder, we could check the current assembly version we used.

如果修改后的组装日期为2/9/2017,则软件包版本为4.3.1.如果程序集的修改日期为2017年4月19日,则软件包版本为4.3.2.如果该程序集不是最新版本,则可以在我这方面正常工作.

If the modified date of assembly is 2/9/2017, the package version is 4.3.1. If the modified date of assembly is 4/19/2017, the package version is 4.3.2. If the assembly is not the latest version, it could work fine on my side.

此外,在创建Azure函数时,默认情况下会安装Microsoft.Asp.Net.WebApi.Client程序包. System.Net.Http是其依赖项之一.因此,我们不需要手动安装System.Net.Http包.在运行我们的应用程序时,NuGet将为我们的应用程序选择正确版本的System.Net.Http.

In addition, Microsoft.Asp.Net.WebApi.Client package is installed by default when creating an Azure function. System.Net.Http is one of its dependencies. So we don't need to install the System.Net.Http package manually. When running our application, NuGet will choose a right version of System.Net.Http for our application.

这篇关于在运行时在Azure Function中找不到HttpRequestMessageExtensions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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