可以在 IHttpModule 上实现 .NET 事件吗?

发布于 2024-10-01 11:40:53 字数 1124 浏览 9 评论 0原文

我已经在 HTTP 模块上声明了一个事件,因此它将轮询订阅者的真/假值,以确定是否应该继续执行调整 HTTP 响应的任务。如果只有一个订阅者回答 true,那么它就会运行其逻辑。

这有道理吗?
是否存在我没​​有看到的潜在陷阱?

public class ResponseTweaker : IHttpModule {

    // to be a list of subscribers 
    List<Func<HttpApplication, bool>> listRespondants = new List<Func<HttpApplication, bool>>();

    // event that stores its subscribers in a collection
    public event Func<HttpApplication, bool> RequestConfirmation {
        add {
            listRespondants.Add(value);
        }
        remove {
            listRespondants.Remove(value);
        }
    }

    public void Init(HttpApplication context) {
        if (OnGetAnswer(context)) // poll subscribers ...
            // Conditionally Run Module logic to tweak Response ... 
    }

    /* Method that polls subscribers and returns 'true'
     *  if only one of them answers yes.
     */
    bool OnGetAnswer(HttpApplication app) {
        foreach (var respondant in listRespondants)
            if (respondant(app))
                return true;
        return false;
    }

    // etc...
}

I've declared an event on an HTTP Module so it will poll subscribers for a true/false value to determine if it should go ahead with its task of tweaking the HTTP Response. If only one subscriber answers true then it runs its logic.

Does this make sense?
Are there potential pitfalls I'm not seeing?

public class ResponseTweaker : IHttpModule {

    // to be a list of subscribers 
    List<Func<HttpApplication, bool>> listRespondants = new List<Func<HttpApplication, bool>>();

    // event that stores its subscribers in a collection
    public event Func<HttpApplication, bool> RequestConfirmation {
        add {
            listRespondants.Add(value);
        }
        remove {
            listRespondants.Remove(value);
        }
    }

    public void Init(HttpApplication context) {
        if (OnGetAnswer(context)) // poll subscribers ...
            // Conditionally Run Module logic to tweak Response ... 
    }

    /* Method that polls subscribers and returns 'true'
     *  if only one of them answers yes.
     */
    bool OnGetAnswer(HttpApplication app) {
        foreach (var respondant in listRespondants)
            if (respondant(app))
                return true;
        return false;
    }

    // etc...
}

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

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

发布评论

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

评论(1

薄暮涼年 2024-10-08 11:40:53

我认为这不是一个好主意。问题的数量取决于一些因素,例如...

  1. listRespondants 将被扎根,因此将具有应用程序生命周期。如果添加了一堆项目,内存占用将不断增加。因此,它宁愿归结为此列表中的项目数量。

以下内容可能是一个阻碍...

  1. IISReset 或应用程序域回收将从您的应用程序中删除所有这些信息。您打算如何将这些项目重新纳入此列表中?数据库?

  2. 如果您有一个网络场怎么办?当您尝试横向扩展时,此应用程序将无法按预期工作。原因是...即使您在网络场中的所有服务器上加载了相同的模块,工作进程中的数据也是本地的。因此,所有服务器中的 listRespondants 都会有所不同,除非您从某个数据库加载它。

I don't think it is a good idea. The amount of issues would depend on some factors like...

  1. listRespondants will be rooted and hence will have application lifetime. If there are a bunch of items that get added, the memory footprint would keep on increasing. So, it would rather come down to the number of items in this list.

The following can be a show stopper...

  1. IISReset or Application Domain recycle will remove all this information from your application. How are you planning to bring the items back in this list? Database?

  2. What if you have a Web farm. This application will not work as expected the moment you try to scale out. The reason being... even if you have the same module loaded on all the servers in the web farm the data in Worker Process is local. Hence the listRespondants would be different in all your servers unless you are loading it from some database.

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