Asp.net Core web api middleware Error `The 'Invoke' method's first argument must be of type 'HttpContext'`

Viewed 3043

I have created following middleware in ASP.NET Core WebAPI project.

SignalrQsHeaderMiddleware.cs

public class SignalrQsHeaderMiddleware
{
    private readonly RequestDelegate _next;

    public SignalrQsHeaderMiddleware(RequestDelegate next)
    {
        this._next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        /*
        if (context.Request.Path.Value.Contains("/restaurantHub"))
        {
            var qsHeader = context.Request.Query["access_token"].ToString();
            if (!qsHeader.IsNullOrEmpty())
            {
                context.Request.Headers.Add("Authorization",qsHeader);
            }
        }*/
        await _next.Invoke(context);

    }
}

In Startup.cs added following line.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    //Other services.
    app.UseMiddleware<SignalrQsHeaderMiddleware>();//<-- added here
}

And when i run project following error occur.

The 'Invoke' method's first argument must be of type 'HttpContext'. at Microsoft.AspNetCore.Builder.<>c__DisplayClass3_0.b__0(RequestDelegate next) at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build() at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()

Here You can see the error image

Note: I have try this solution Here

But still i am facing the same issue.

Any suggestion what i am doing wrong ?

2 Answers

Your middleware is the last one, so there is no "next" item. Looks like the bug is that we can see wrong error message. But you must check, if _next != null anyway.

As I understood, the error message says that the Invoke methods first param should be of type HttpContext . See below code for example. The Invoke method's first param is IApplicationBuilder. This creates problem.

 public Task Invoke( IApplicationBuilder app, HttpContext httpContext)
        {
            app.UseExceptionHandler(error =>
            {
                error.Run(async context =>
                {
                    await context.Response.WriteAsync("from Exception Handler middleware");
                });
            });
            return  _next(httpContext);
        }

Changed this to below. My issue got fixed. It should always be HttpContext type as first parameter for Invoke.

public Task Invoke(HttpContext httpContext, IApplicationBuilder app)
        {
            app.UseExceptionHandler(error =>
            {
                error.Run(async context =>
                {
                    await context.Response.WriteAsync("from Exception Handler middleware");
                });
            });
            return  _next(httpContext);
        }

Please check in your custom middleware if your parameter order is placed as above. I can't see anything on the current middleware of the code you shared. I would suggest you to check in other middlewares as well for the order.

Related