Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of IStartupFilter in asp.net core?

Tags:

asp.net-core

IStartupFilter is the basis of a mechanism for libraries to add middleware to the app. According to the Docs "IStartupFilter is useful to ensure that a middleware runs before or after middleware added by libraries at the start or end of the app's request processing pipeline".

Does the mechanism allow you to manipulate the pipeline in any way that can't be done from Startup.Configure()?

If the point is modularity then you just seem to be trading coupling through Startup.Configure() for coupling via the IServicesCollection (a call to DI is required). In the simple case (as per the example) a call to services.AddTransient<IStartupFilter, ...>() can be removed from ConfigureServices() and app.AddMiddleware<MyMiddleware>() can be added to achieve the same functionality with less complexity and magic.

Is the main point of the mechanism to allow the library to apply conditions as to what middleware should be included? If so, it seems to lack asp.net core's customary economy and clarity of design.

like image 432
mikemay Avatar asked Jul 20 '18 07:07

mikemay


People also ask

What is the use of startup class in .NET Core?

The Startup class in .NET and .NET Core The Startup class contains the ConfigureServices and Configure methods. While the former is used to configure the required services, the latter is used to configure the request processing pipeline. The Configure method is executed immediately after the ConfigureServices method.

What is startup file in .NET Core?

The Startup class ASP.NET Core apps use a Startup class, which is named Startup by convention. The Startup class: Optionally includes a ConfigureServices method to configure the app's services. A service is a reusable component that provides app functionality.

What is the role of ConfigureServices and configure method?

Use ConfigureServices method to add services to the container. Use Configure method to configure the HTTP request pipeline.

What is the difference between IApplicationBuilder use () and IApplicationBuilder run ()?

Use() and IApplicationBuilder. Run() C# Asp.net Core? We can configure middleware in the Configure method of the Startup class using IApplicationBuilder instance. Run() is an extension method on IApplicationBuilder instance which adds a terminal middleware to the application's request pipeline.


2 Answers

In the simple case (as per the example) a call to services.AddTransient<IStartupFilter, ...>() can be removed from ConfigureServices() and app.AddMiddleware() can be added to achieve the same functionality with less complexity and magic.

That's not true.

There is a big difference between using IStartupFilter and using a middleware. A middleware is part of the request pipeline, which means it gets executed on every request. On the other hand, IStartupFilter gets executed just once when the application starts (not on every request).

like image 133
Alisson Reinaldo Silva Avatar answered Oct 17 '22 18:10

Alisson Reinaldo Silva


To answer my own question, I think that the main use case is to enable the framework to encorporate assemblies that were not known at build time. The docs cover this in the aspnetcore/Fundamentals/Host nhance an app from an external assembly in ASP.NET Core with IHostingStartup. (Although the documentation makes no mention of IStartupFilter the associated StartupDiagnostics sample project does.

like image 44
mikemay Avatar answered Oct 17 '22 20:10

mikemay