I have one custom middleware which i have to configured like this way if we disable it from backend then that middleware should detach from request pipeline and if i enable then it should i again attached to request pipeline but during this process do i have to restart or recycle application pool on server? check below example.
class Startup{
public void Configure(IApplicationBuilder app)
{
if(isCustomMiddlewareEnable){
app.Custommiddleware()
}
}
}
CodePudding user response:
In short - yes, because Configure()
as well as ConfigureServices()
are called once during application startup. If the value behind isCustomMiddlewareEnable
changes, i.e. you change a key's value in a configuration file with file reload enabled, that will still not affect your application behavior, having that configured as shown in question.
What you are looking for is a runtime evaluated if statement that redirects the request pipeline through a middleware if the condition is met.
app.UseWhen(
context => context.Request.Method == HttpMethod.Post.Method,
builder => builder.Custommiddleware());
Alternatively, you can always go into the middleware, but execute your custom logic inside only if the condition is met:
public class CustomMiddleware
{
private readonly RequestDelegate _next;
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context, IOptions<CustomMiddlewareOptions> customOptions)
{
if (customOptions.Value.IsEnabled)
{
// Do some custom work.
}
await _next(context);
}
}