Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in ASP.NET Core, is there any way to set up middleware from Program.cs?

I am building a support library for ASP.NET Core websites. I have a few pieces of middleware that need to be enabled, and they need to be added before any other middleware due what they do.

I can create an extension method on IWebHostBuilder to add services, likewise for configuring logging, but I don't see any way to add middleware in a programmatic way. Is there any way to do this? Looking at the source for WebHost/WebHostBuilder nothing jumped out.


Given the first comment, I may not have been clear enough. I know how to create middleware and use it. What I am trying to do is ensure that when the Configure(IApplicationBuilder app) method is called on Startup by the framework, my middleware is already in place. In a similar manner to being able to do ServiceConfiguration prior to Startup even being created. So an extension method like

public static IWebHostBuilder AddPayscaleHostingServices(this IWebHostBuilder webHostBuilder, string serviceName)
{
    return webHostBuilder.ConfigureServices(collection =>
    {
        collection.RegisterPayscaleHostingServices();
    }).ConfigureLogging(factory =>
    {

    });
}

gives me the ability to do some setup prior to the webHostBuilder.Build method, but I don't see anything similar for middleware/anything on IApplicationBuilder.

Thanks, Erick

like image 337
Erick T Avatar asked Apr 25 '17 06:04

Erick T


People also ask

Can we create custom middleware in .NET Core?

We can add middleware using app. UseMiddleware<MyMiddleware>() method of IApplicationBuilder also. Thus, we can add custom middleware in the ASP.NET Core application.

Which method from the startup CS class is used to configure middleware in an ASP NET MVC web application?

The web host is created in the main method of the Program class and here we have configuration of startup class using method "UseStartup".

What is the purpose of Program CS in .NET Core?

ASP.NET Core web application is actually a console project which starts executing from the entry point public static void Main() in Program class where we can create a host for the web application.


1 Answers

You could use a startup filter to achieve this. Startup filters allow you to configure middleware from a service resolved from the DI container.

Defining a startup filter is easy:

public class MyStartupFilter : IStartupFilter
{
    public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
    {
        return app =>
        {
            // Configure middleware
            // ...

            // Call the next configure method
            next(app);
        };
    }
}

Always make sure to call next(app) or any other middleware won't be configured.

Now register the startup filter as a singleton implementation of IStartupFilter in your ConfigureServices method:

services.AddSingleton<IStartupFilter, MyStartupFilter>();
like image 127
Henk Mollema Avatar answered Sep 29 '22 01:09

Henk Mollema