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

Viewed 2066

I'm in the middle of migrating from .NET Core 3.1 to .NET 5.0 for my Azure Functions project. I have the following function 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)
{
    ...
}

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.

How can I fix this error?

1 Answers

After reading the Microsoft Documentation about the Execution Context, I see the class was renamed to FunctionContext

The fix to the issue was simple, rename ExecutionContext to FunctionContext. I realize that the Microsoft.Azure.WebJobs is not needed for .NET 5 Azure Functions. Next, I needed to change the type from System.Net.Http.HttpRequestMessage to Microsoft.Azure.Functions.Worker.Http.HttpRequestData. Lastly, I removed the ILogger in favor of the GetLogger Method.

The final function looks like this.

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.

Related