[英]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.