可以在 IHttpModule 上实现 .NET 事件吗?
我已经在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是一个好主意。问题的数量取决于一些因素,例如...
以下内容可能是一个阻碍...
IISReset 或应用程序域回收将从您的应用程序中删除所有这些信息。您打算如何将这些项目重新纳入此列表中?数据库?
如果您有一个网络场怎么办?当您尝试横向扩展时,此应用程序将无法按预期工作。原因是...即使您在网络场中的所有服务器上加载了相同的模块,工作进程中的数据也是本地的。因此,所有服务器中的 listRespondants 都会有所不同,除非您从某个数据库加载它。
I don't think it is a good idea. The amount of issues would depend on some factors like...
The following can be a show stopper...
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?
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.