Search code examples
c#azureazure-functionsazure-http-triggerazure-functions-isolated

Getting exception "Cannot dynamically create an instance of type 'Microsoft.Azure.Functions.Worker.Http.HttpRequestData'." in Azure function


I am trying to create my first Azure function. The function will be created using C# and .Net 8.0. It will run in the isolated worker model mode, and use a HttpTrigger. The HTTP request will be a POST, containing some JSON in the body.

I have looked at several examples, that use the HttpRequestData. If I try to run an example function, and send a request (using Postman) immediately an internal server error is returned (status code 500). The error message is:

System.Private.CoreLib: Exception while executing function: MyHttpTriggerFunction. Microsoft.Azure.WebJobs.Host: One or more errors occurred. (Exception binding parameter 'req') (Exception binding parameter 'functionContext'). Exception binding parameter 'req'. System.Private.CoreLib: Cannot dynamically create an instance of type 'Microsoft.Azure.Functions.Worker.Http.HttpRequestData'. Reason: Cannot create an abstract class.

The signature of my test function is:

        [FunctionName(nameof(MyHttpTriggerFunction))]
        public static HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]
            HttpRequestData req,
            FunctionContext functionContext)

Why am I getting the exception?


Solution

  • After more digging around on the Internet, I discovered I had to replace some NuGet packages that VS added as part of the creation of the function app: Microsoft.Azure.WebJobs vs. Microsoft.Azure.Functions.

    I also replaced the FunctionName attribute with Function.

    And the last thing was to update the Azure tools in VS (as this apparently does not happen as part of the regular updates).