开发者

How can I register generic class as IHttpModule

开发者 https://www.devze.com 2023-01-24 10:53 出处:网络
publ开发者_开发百科ic class NHibernateSessionPerRequest<T>:IHttpModule { public void Dispose() { }
publ开发者_开发百科ic class NHibernateSessionPerRequest<T>:IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += BeginRequest;
        context.EndRequest += EndRequest;
    }
}

How can it be registered in web.config?


IIS6 : In the system.web section of your web.config

<configuration>
  <system.web>
    <httpModules>
      <add name="MyModule" type="My.Namespace.NHibernateSessionPerRequest`1[[My.Namespace.MyType, My.Assembly]], My.Assembly"/>
    </httpModules>
  </system.web>
</configuration>

IIS7 : In the system.webserver section of your web.config :

<configuration>
  <system.webServer>
    <modules>
      <add name="MyModule" type="My.Namespace.NHibernateSessionPerRequest`1[[My.Namespace.MyType, My.Assembly]], My.Assembly"/>
    </modules>
  </system.webServer>
</configuration>

For both, replace "My.Namespace" with the namespace of the class, and "My.Assembly" with the assembly name.


The easiest solution is to create a concrete type that derives from this type, and then register that:

public class MyActualModule : NHibernateSessionPerRequest<SomeType> {
}

Then in web.config register MyActualModule.

Alternatively, you can use the CLR-compliant syntax for generic types, but it's so awful that I won't even mention it here. Even a simple type like yours will have a type name that's probably over 100 chars long.

0

精彩评论

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