如何正确处置IHttpModule?
我看到的 IHttpModule 的所有实现如下所示:
class HttpCompressionModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.SomeEvent += OnSomeEvent;
}
private void OnSomeEvent(Object source, EventArgs e)
{
// ...
}
public void Dispose()
{
// nothing here !!!
}
}
我想知道为什么 Dispose
方法总是为空?难道我们不应该在 Init
方法中取消订阅我们订阅的事件吗?
All implementation of IHttpModule I've seen looks following:
class HttpCompressionModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.SomeEvent += OnSomeEvent;
}
private void OnSomeEvent(Object source, EventArgs e)
{
// ...
}
public void Dispose()
{
// nothing here !!!
}
}
I am wondering why is the Dispose
method always empty? Shouldn't we unsubscribe the event which we subscribe in the Init
method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HttpModule 的生命周期与 HttpApplication 的生命周期紧密集成。 HttpModule 的实例在应用程序启动时生成,并在应用程序废弃时销毁。
在这种情况下,取消订阅事件是没有意义的,因为发布者 (HttpApplication) 无论如何都会被丢弃。当然,在发布者没有被处置的情况下,取消事件处理程序的挂钩将是正确的做法。
The lifecycle of an HttpModule is tightly integrated with the lifecycle of an HttpApplication. Instances of HttpModule are generated when the application is started and destroyed when the application is disposed of.
In this case there is no point in unsubscribing from the event because the publisher (HttpApplication) is being disposed of anyway. Of course, in a situation where the publisher wasn't being disposed of, unhooking the event handler would be the right thing to do.
如果您需要在模块内实例化 IDisposable 对象,则 dispose 方法不会为空。
The dispose method won't be empty if you need to instantiate IDisposable objects inside your module.