https://github.com/dazinator/reconfigurablemiddleware
enhancing ASP.NET Core applications by introducing a middleware pipeline capable of responding to configuration changes with dynamic rebuilding.
https://github.com/dazinator/reconfigurablemiddleware
Last synced: about 1 year ago
JSON representation
enhancing ASP.NET Core applications by introducing a middleware pipeline capable of responding to configuration changes with dynamic rebuilding.
- Host: GitHub
- URL: https://github.com/dazinator/reconfigurablemiddleware
- Owner: dazinator
- Created: 2024-03-31T10:27:43.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-31T11:25:46.000Z (about 2 years ago)
- Last Synced: 2024-04-14T07:12:05.072Z (about 2 years ago)
- Language: C#
- Size: 28.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Reconfigurable Middleware
Allow middleware in your asp.net core application to be re-configured, by reloading it when configuration changes.
## Example
appsettings.json:
```json
{
"Pipeline": {
"UseDeveloperExceptionPage": false
}
}
```
startup.cs:
```csharp
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
var configSection = Configuration.GetSection("Pipeline");
services.Configure(configSection);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Note: Use vs Run (latter is terminal, former is not)
// make a change to appsettings.json "Pipelines" section and watch log output in console on furture requests.
app.UseReloadablePipeline(ConfigureReloadablePipeline);
app.UseWelcomePage();
}
private void ConfigureReloadablePipeline(IApplicationBuilder appBuilder, IWebHostEnvironment environment, PipelineOptions options)
{
var logger = appBuilder.ApplicationServices.GetRequiredService>();
logger.LogInformation("Building reloadable pipeline from current options!");
if (options.UseDeveloperExceptionPage)
{
appBuilder.Use(async (context, onNext) =>
{
var logger = context.RequestServices.GetRequiredService>();
logger.LogInformation("Using dev middleware!");
await onNext();
});
}
else
{
appBuilder.Use(async (context, onNext) =>
{
var logger = context.RequestServices.GetRequiredService>();
logger.LogInformation("Not using dev middleware..");
await onNext();
});
}
}
}
```