distup.cs文件是否需要应用程序池重新启动或回收启动。

发布于 2025-02-08 03:12:57 字数 323 浏览 1 评论 0原文

我有一个自定义中间件,如果我们从后端禁用它,我必须像以这种方式进行配置,那么中间件应该从请求管道上脱离,如果我启用了,则我应该再次附加到请求管道,但是在此过程中,我必须重新启动或重新启动或在服务器上回收应用程序池?在下面查看示例:

 class Startup{

 public void Configure(IApplicationBuilder app)
        {
            if(isCustomMiddlewareEnable){
              app.Custommiddleware()
            }
}
    }

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()
            }
}
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦与时光遇 2025-02-15 03:12:57

简而言之 - 是的,因为configure()以及configureservices()在应用程序启动期间一次调用一次。如果iScustommiddlewareable更改后面的值,即您在启用文件重新加载的配置文件中更改键的值,这仍然不会影响您的应用程序行为,并将其配置为所示。

您要查找的是评估的运行时,如果要满足条件,请通过中间件重定向管道。

app.UseWhen(
    context => context.Request.Method == HttpMethod.Post.Method, 
    builder => builder.Custommiddleware());

另外,您可以随时进入中间件,但仅在满足条件时才在内部执行自定义逻辑:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文