'Azure Function Middleware NullReferenceException

I've created an (isolated) Azure Function with target framework net6.0 (runtime v4). I added some MiddleWare (based on https://jinalkumarpatel.hashnode.dev/azure-functions-middleware-part-2-authentication-middleware) that checks the bearer token (+ user roles) of a request.

When I'm testing the function locally with Azure Functions Core Tools and multiple requests are fired within seconds I get a NullReferenceException (see line 32 in the screenshot below). I have a hard time figuring out what the problem is, because 'next' en 'context' are not null.

If I remove 'await' on line 32 then the NullReferenceException does not appear.

Can someone explain to me what is happening here?

enter image description here

    public class BearerAuthenticationMiddleware : IFunctionsWorkerMiddleware
    {
        private readonly ISharedService sharedService;

        public BearerAuthenticationMiddleware(ISharedService sharedService) 
        {
            this.sharedService = sharedService;
        }
        public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
        {
            if (context.IsHttpTriggerFunction())
            {
                string headers = context.BindingContext.BindingData["Headers"]?.ToString();
                var httpHeaders = headers == null ? null : System.Text.Json.JsonSerializer.Deserialize<HttpHeaders>(headers);
                
                //Validation logic for token
                if (httpHeaders?.Authorization != null && await sharedService.IsValidUser(httpHeaders.Authorization)) // only continue with valid Ixon token and right user / group
                {
                    await next(context);
                }
                else
                {
                    await context.CreateJsonResponse(System.Net.HttpStatusCode.Unauthorized, new { Message = "Token is not valid." });
                }
            }
            else
            {
                await next(context);
            }
        }
    }

Stack trace:

at Microsoft.Azure.Functions.Worker.Invocation.TaskMethodInvoker2.InvokeAsync(TReflected instance, Object[] arguments) in D:\a\1\s\src\DotNetWorker.Core\Invocation\TaskMethodInvoker.cs:line 23 at Microsoft.Azure.Functions.Worker.Invocation.DefaultFunctionInvoker2.InvokeAsync(Object instance, Object[] arguments) in D:\a\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionInvoker.cs:line 31 at Microsoft.Azure.Functions.Worker.Invocation.DefaultFunctionExecutor.d__4.MoveNext() in D:\a\1\s\src\DotNetWorker.Core\Invocation\DefaultFunctionExecutor.cs:line 37 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Microsoft.Azure.Functions.Worker.OutputBindings.OutputBindingsMiddleware.d__0.MoveNext() in D:\a\1\s\src\DotNetWorker.Core\OutputBindings\OutputBindingsMiddleware.cs:line 15 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at BackendFunctionApp.Middleware.BearerAuthenticationMiddleware.d__2.MoveNext() in .\src\BackendFunctionApp\Middleware\BearerAuthenticationMiddleware.cs:line 32



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source