在运行时编程方式注册的HttpModules [英] Programmatically register HttpModules at runtime

查看:116
本文介绍了在运行时编程方式注册的HttpModules的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序,其中的第三方供应商可以编写插件的DLL,并将其拖放到Web应用程序的bin目录。我要为这些插件,能够在必要时注册自己的HttpModules的能力。

反正是有,我可以添加或往返于管道在运行时删除的HttpModules,而无需在web.config相应的条目,或者我有添加/删除模块时以编程方式编辑web.config?我知道,无论哪种方式会导致一个AppDomain重新启动,但我宁愿能做到它在code不必捏造web.config中达到同样的效果。


解决方案

  

它在正确的做
  时间在HttpApplication的生命周期
  这是一个HttpApplication时
  对象初始化(多次,
  一次的每个实例
  HttpApplication的)。唯一的方法
  其中,这工作正确的是
  的HttpApplication的init()


  
  

要挂钩通过code,可一个模块
  运行code像下面的,而不是
  该HTTP模块定义
  web.config中:

 公共类全球:System.Web.HttpApplication
  {
     //一些模块使用显式接口实现
     //通过声明该静态成员作为IHttpModule接口
     //我们解决了
     公共静态IHttpModule的模块=新xrnsToashxMappingModule();
     公共覆盖无效的init()
     {
         base.Init();
         Module.Init(本);
     }
  }

所有你要做的就是覆盖的HttpApplication的init()方法
  然后访问静态实例的初始化
  方法。的init()的模块挂接
  事件和您去。


斯特劳的博客

I'm writing an app where 3rd party vendors can write plugin DLLs and drop them into the web app's bin directory. I want the ability for these plugins to be able to register their own HttpModules if necessary.

Is there anyway that I can add or remove HttpModules from and to the pipeline at runtime without having a corresponding entry in the Web.Config, or do I have to programmatically edit the Web.Config when adding / removing modules? I know that either way is going to cause an AppDomain restart but I'd rather be able to do it in code than having to fudge the web.config to achieve the same effect.

解决方案

It has to be done at just the right time in the HttpApplication life cycle which is when the HttpApplication object initializes (multiple times, once for each instance of HttpApplication). The only method where this works correct is HttpApplication Init().

To hook up a module via code you can run code like the following instead of the HttpModule definition in web.config:

  public class Global : System.Web.HttpApplication
  {
     // some modules use explicit interface implementation
     // by declaring this static member as the IHttpModule interface
     // we work around that
     public static IHttpModule Module = new xrnsToashxMappingModule();
     public override void Init()
     {
         base.Init();
         Module.Init(this);
     }
  }

All you do is override the HttpApplication's Init() method and then access the static instance's Init method. Init() of the module hooks up the event and off you go.

Via Rick Strahl's blog

这篇关于在运行时编程方式注册的HttpModules的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆