不使用配置文件动态注册HttpModule

时间:2022-07-24 13:36:14

在asp.net 4.0中,提供了一种不通过修改配置文件注册Module的方法。从.net3.5开始,新提供的PreApplicationStartMethodAttribute特性可以应用在程序集上,使得自定义的网站初始化代码可以在web应用程序的Application_Start初始化环节之前就执行。这个步骤甚至在动态编译和执行Application_Start之前。对于每个程序集,可以定义一次,PreApplicationStartMethodAttribute定义如下:

#region Assembly System.Web.dll, v4.0.0.0
// C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Web.dll
#endregion using System; namespace System.Web
{
// Summary:
// Provides expanded support for application startup.
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public sealed class PreApplicationStartMethodAttribute : Attribute
{
// Summary:
// Initializes a new instance of the System.Web.PreApplicationStartMethodAttribute
// class.
//
// Parameters:
// type:
// An object that describes the type of the startup method..
//
// methodName:
// An empty parameter signature that has no return value.
public PreApplicationStartMethodAttribute(Type type, string methodName); // Summary:
// Gets the associated startup method.
//
// Returns:
// A string that contains the name of the associated startup method.
public string MethodName { get; }
//
// Summary:
// Gets the type that is returned by the associated startup method.
//
// Returns:
// An object that describes the type of the startup method.
public Type Type { get; }
}
}

Type用来指定定义了初始化方法的类型,MethodName用来指定将要执行的初始化方法。

通过这种方式,我们可以不在配置文件中固定配置HttpModule,而是定义一个方法,这个方法可以返回需要动态注册的HttpModule,将这个方法以委托的方式等级在一个集合中。在网站启动之前后,每当HttpApplicationFactory创建一个HttpApplication对象,完成正常注册的HttpModule创建及初始化之后,再来创建我们动态注册的这些HttpModule。

对于HttpApplication来说,其Init方法将在网站正常注册的HttpModule创建及注册之后被调用,用来完成自定义的HttpApplication初始化。我们可以在这个时间点动态注册HttpModule。在asp.net网站中,Global.asax文件用来生成一个HttpApplication的派生类。这个类用来创建网站中使用的HttpApplication对象,这就为我们提供了一个时间点,我们可以重写这个派生类的Init方法,完成动态注册HttpModule创建和注册工作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState; namespace HttpRequestDemo
{
public class Global : System.Web.HttpApplication
{
private List<IHttpModule> dynamicModules;
public override void Init()
{
base.Init();
this.dynamicModules = DynamicHttpModuleManager.GetModules();
foreach (var item in this.dynamicModules)
{
item.Init(this);
}
}
protected void Application_Start(object sender, EventArgs e)
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace UserModule
{
public delegate IHttpModule CreateDynamicHttpModule();
public static class DynamicHttpModuleManager
{
private static List<CreateDynamicHttpModule> _createModuleHandlerList = new List<CreateDynamicHttpModule>();
/// <summary>
/// 在网站初始化之前,将需要注册的Module类型记录在一个集合中
/// </summary>
/// <param name="handler"></param>
public static void RegisterDynamicModule(CreateDynamicHttpModule handler)
{
_createModuleHandlerList.Add(handler);
}
/// <summary>
/// 获取要注册的HttpModule
/// </summary>
/// <returns></returns>
public static List<IHttpModule> GetModules()
{
List<IHttpModule> lst = new List<IHttpModule>();
foreach (var item in _createModuleHandlerList)
{
IHttpModule module = item();
lst.Add(module);
}
return lst;
}
} }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace UserModule
{
//自定义的HttpModule中,添加一个Rgister方法,用来注册
public class OnlineUserModule:IHttpModule
{
public static void Register()
{
DynamicHttpModuleManager.RegisterDynamicModule(() => new OnlineUserModule());
}
public void Dispose()
{
throw new NotImplementedException();
} public void Init(HttpApplication context)
{
throw new NotImplementedException();
}
}
}

最后在项目的AssemblyInfo.cs文件中增加PreApplicationStartMethod特征完成动态注册。

[assembly: PreApplicationStartMethod(typeof(UserModule.OnlineUserModule), "Register")]

注意这里的AssemblyInfo.cs是指的你的程序集的。在里面添加这个PreApplicationStartMethod。然后运行web项目,你会发现,断点会进入你的自定义的HttpModule。