简体   繁体   English

将 .NET Core 3.1 迁移到 .NET 5.0,AzureFunctions,无法将输入参数 'req' 转换为类型 'System.Net.Http.HttpRequestMessage

[英]Migrating .NET Core 3.1 to .NET 5.0, AzureFunctions, Cannot convert input parameter 'req' to type 'System.Net.Http.HttpRequestMessage'

I'm in the middle of migrating from .NET Core 3.1 to .NET 5.0 for my Azure Functions project.对于我的 Azure Functions 项目,我正在从 .NET Core 3.1 迁移到 .NET 5.0。 I have the following function decleration:我有以下 function 声明:

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;

[Function("Test")]
public static async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, 
    ILogger log, 
    Microsoft.Azure.WebJobs.ExecutionContext context)
{
    ...
}

However, I receive the following error message:但是,我收到以下错误消息:

System.Private.CoreLib: Exception while executing function: Functions.SaveBlob. System.Private.CoreLib: Result: Failure
Exception: Microsoft.Azure.Functions.Worker.Diagnostics.Exceptions.FunctionInputConverterException: Error converting 1 input parameters for Function 'Test': Cannot convert input parameter 'req' to type 'System.Net.Http.HttpRequestMessage' from type 'Microsoft.Azure.Functions.Worker.GrpcHttpRequestData'.

I need to keep the ExecutionContext for my application.我需要为我的应用程序保留 ExecutionContext。

How can I fix this error?我该如何解决这个错误?

After reading the Microsoft Documentation about the Execution Context , I see the class was renamed to FunctionContext阅读有关执行上下文的 Microsoft 文档后,我看到 class 已重命名为FunctionContext

The fix to the issue was simple, rename ExecutionContext to FunctionContext .该问题的解决方法很简单,将ExecutionContext重命名为FunctionContext I realize that the Microsoft.Azure.WebJobs is not needed for .NET 5 Azure Functions.我意识到 .NET 5 Azure 功能不需要Microsoft.Azure.WebJobs Next, I needed to change the type from System.Net.Http.HttpRequestMessage to Microsoft.Azure.Functions.Worker.Http.HttpRequestData .接下来,我需要将类型从System.Net.Http.HttpRequestMessage更改为Microsoft.Azure.Functions.Worker.Http.HttpRequestData Lastly, I removed the ILogger in favor of the GetLogger Method .最后,我删除了ILogger以支持GetLogger Method

The final function looks like this.最终的 function 看起来像这样。

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;

[Function("Test")]
public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestData req
        FunctionContext context)
{
    ...
}

When upgrading, be sure to thoroughly read all their documentation about the new changes .升级时,一定要彻底阅读他们所有关于新变化的文档 It also helped to create a new project through Visual Studio Code using .NET 5 to see how a new project is structured.它还有助于使用 .NET 5 通过 Visual Studio Code 创建一个新项目,以了解新项目的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从“字符串”转换为“ System.Net.Http.HttpRequestMessage” - Cannot convert from 'string' to 'System.Net.Http.HttpRequestMessage' Visual Studio 找不到 System.Net.Http.HttpRequestMessage class - Visual Studio cannot find System.Net.Http.HttpRequestMessage class 如何使用 System.Net.Http.HttpRequestMessage 检索 Cookies - How to retrieve Cookies with System.Net.Http.HttpRequestMessage “未找到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()' - "Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.Controllers.HttpActionContext.get_Request()' .NET Core 3.1 / .NET 5.0 中的 System.Security.CodeAccessPermission 是否已过时? - Is System.Security.CodeAccessPermission obsolete in .NET Core 3.1 / .NET 5.0? 从.Net Core 3.1 迁移到 .NET 5 - Migrating from .Net Core 3.1 to .NET 5 将 ASP.NET Core 3.1 迁移到 .NET 5.0 - Migrate ASP.NET Core 3.1 to .NET 5.0 找不到HttpRequestMessage,但引用了System.Net.Http - HttpRequestMessage could not be found, yet System.Net.Http is referenced ASP.Net核心传递HttpRequestMessage参数始终为空 - ASP.Net Core Passing HttpRequestMessage Parameter always empty 将 asp.net 内核 3.1 迁移到 .net 6 错误 - Migrating the asp.net core 3.1 to .net 6 error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM