ExtendStartup.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
SLIDE-2
Use IStartupFilter:
- To configure middleware at the beginning or end of an app's Configure middleware pipeline without an explicit call to Use{Middleware}.
IStartupFilter
is used by ASP.NET Core to add defaults to the beginning of the pipeline without having to make the app author explicitly register the default middleware.IStartupFilter
allows a different component to call Use{Middleware} on behalf of the app author. - To create a pipeline of Configure methods.
IStartupFilter.Configure
can set a middleware to run before or after middleware added by libraries.
IStartupFilter
implements Configure, which receives and returns an Action<IApplicationBuilder>
. An IApplicationBuilder
defines a class to configure an app's request pipeline. For more information, see Create a middleware pipeline with IApplicationBuilder
.
Each IStartupFilter
can add one or more middlewares
in the request pipeline. The filters are invoked in the order they were added to the service container. Filters may add middleware before or after passing control to the next filter, thus they append to the beginning or end of the app pipeline.
SLIDE-3
The following example demonstrates how to register a middleware with IStartupFilter
. The RequestSetOptionsMiddleware
middleware sets an options value from a query string parameter:
public class RequestSetOptionsMiddleware
{
private readonly RequestDelegate _next;
public RequestSetOptionsMiddleware( RequestDelegate next )
{
_next = next;
}
// Test with https://localhost:5001/Privacy/?option=Hello
public async Task Invoke(HttpContext httpContext)
{
var option = httpContext.Request.Query["option"];
if (!string.IsNullOrWhiteSpace(option))
{
httpContext.Items["option"] = WebUtility.HtmlEncode(option);
}
await _next(httpContext);
}
}
SLIDE-4
The RequestSetOptionsMiddleware is configured in the RequestSetOptionsStartupFilter class:
public class RequestSetOptionsStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return builder =>
{
builder.UseMiddleware<RequestSetOptionsMiddleware>();
next(builder);
};
}
}
SLIDE-5
The IStartupFilter is registered in the service container in ConfigureServices.
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.ConfigureServices(services =>
{
services.AddTransient<IStartupFilter,
RequestSetOptionsStartupFilter>();
});
}
SLIDE-6
When a query string parameter for option
is provided, the middleware processes the value assignment before the ASP.NET Core middleware renders the response.
Middleware execution order is set by the order of IStartupFilter registrations:
-
Multiple IStartupFilter implementations may interact with the same objects. If ordering is important, order their IStartupFilter service registrations to match the order that their middlewares should run.
-
Libraries may add middleware with one or more IStartupFilter implementations that run before or after other app middleware registered with IStartupFilter. To invoke an IStartupFilter middleware before a middleware added by a library's IStartupFilter:
- Position the service registration before the library is added to the service container.
- To invoke afterward, position the service registration after the library is added.