Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why middleware in ASP.NET Core requires specific semantics, but not an interface?

As known, IApplicationBuilder of the method Configure (class Startup) in ASP.NET Core requires specific semantics (to have method 'Invoke' with input parameter of HttpContext type and Task as return value). But why it's not implemented as interface? I can write something like it:

public class FakeMiddleware
{

}

and register it:

    app.UseMiddleware<FakeMiddleware>();

and we'll get an runtime error. Of course, it's trivial thing and easy to be found and fix, but it's implemented so rough, without interface?

like image 968
Oleg Sh Avatar asked Nov 30 '16 20:11

Oleg Sh


People also ask

Why do we need middleware in .NET Core?

Middleware in ASP.NET Core controls how our application responds to HTTP requests. It can also control how our application looks when there is an error, and it is a key piece in how we authenticate and authorize a user to perform specific actions.

How does middleware work in NET Core?

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.

What is difference between middleware and filters in .NET Core?

Middleware only has access to the HttpContext and anything added by preceding middleware. In contrast, filters have access to the wider MVC context, so can access routing data and model binding information for example.

What is the difference between app run and app use middleware in .NET Core?

Run() will end the request, and app. Use() will pass the request to next middleware.


2 Answers

The Invoke method is flexible and you can ask for additional parameters. ASP.NET will inject the additional parameters using the application's service configuration.

public async Task Invoke(HttpContext ctx, 
                         IHostingEnvironment host,
                         ISomethingElse service)
{
    // ...
}

C# interface definitions can't provide this flexibility in a nice way.

like image 153
OdeToCode Avatar answered Oct 19 '22 07:10

OdeToCode


Since AspNetCore2.0 you can set middleware which implements interface IMiddleware.

public class InterfaceMiddleware : IMiddleware
{
    private InterfaceMiddlewareOptions _opts;

    public InterfaceMiddleware(IOptions<InterfaceMiddlewareOptions> opts)
    {
        _opts = opts.Value;
    }

    public async Task InvokeAsync(HttpContext context, RequestDelegate next)
    {
        await context.Response.WriteAsync(_opts.Message);
    }
}

In addition to app.UseMiddleware<InterfaceMiddleware>(). You need to register your middleware in DI(singleton lifetime is not required).

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<InterfaceMiddlewareOptions>(opts =>
    {
        opts.Message = "IMiddleware interface is implemented";
    });

    services.AddSingleton<InterfaceMiddleware>();
}
like image 21
Илья Любашов Avatar answered Oct 19 '22 06:10

Илья Любашов