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

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

问题描述

我有一个创建预编译 DLL 的 Azure Function 应用程序(因此它使用普通的 .cs 文件,而不是 VS2017 之前的旧 .csx 方法).以前,它的目标是 .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.

在您更改软件包版本期间,程序集可能不会更新.从 binDebug et47 文件夹中,我们可以检查我们使用的当前程序集版本.

The assembly might not be updated during you change the package version. From the binDebug et47 folder, we could check the current assembly version we used.

如果修改组装日期为2/9/2017,则包版本为4.3.1.如果修改组装日期为 4/19/2017,则包版本为 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.

另外,Microsoft.Asp.Net.WebApi.Client 包在创建 Azure 函数时默认安装.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 函数中运行时未找到 HttpRequestMessageExtensions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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