将 .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'

我正在为我的 Azure Functions 项目从 .NET Core 3.1 迁移到 .NET 5.0。我有以下函数 decleration:

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)
{
    ...
}

但是,我收到以下错误消息:

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'.

我需要为我的应用程序保留 ExecutionContext。

我该如何解决这个错误?

看完Microsoft Documentation about the Execution Context, I see the class was renamed to FunctionContext

问题的解决很简单,将 ExecutionContext 重命名为 FunctionContext。我意识到 .NET 5 Azure Functions 不需要 Microsoft.Azure.WebJobs。接下来,我需要将类型从 System.Net.Http.HttpRequestMessage 更改为 Microsoft.Azure.Functions.Worker.Http.HttpRequestData。最后,我删除了 ILogger 以支持 GetLogger Method.

最终函数如下所示。

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)
{
    ...
}

升级时一定要彻底read all their documentation about the new changes。它还有助于通过使用 .NET 5 的 Visual Studio 代码创建新项目,以查看新项目的结构。