开发者

Okay to implement a .NET event on an IHttpModule?

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 开发者_如何学Python...
            // 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 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜