3

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 ?

5
  • 1
    Accordingly to your screenshot, you have RTM libraries. Try to update all the packages to the latest stable ones. Commented Jun 1, 2017 at 14:23
  • 1
    Do you reference the package Microsoft.AspNetCore.Http ? Your code looks great ... Commented Jun 1, 2017 at 14:30
  • @Adrien and @Ilya Chumakov thanks . I Updated all packages as well as set reference Microsoft.AspNetCore.Http. Now it's working fine. :) Commented Jun 6, 2017 at 9:02
  • have you tried just await _next(context)? Commented Aug 19, 2017 at 1:32
  • I think the suspect is obfuscator. Commented Sep 30 at 12:31

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.