在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。

不使用配置文件动态注册HttpModule的更多相关文章

  1. 你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)

    一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什 ...

  2. MVC之前的那点事儿系列(6):动态注册HttpModule

    文章内容 通过前面的章节,我们知道HttpApplication在初始化的时候会初始化所有配置文件里注册的HttpModules,那么有一个疑问,能否初始化之前动态加载HttpModule,而不是只从 ...

  3. 动态注册HttpModule

    动态注册HttpModule 2014-06-05 08:58 by 汤姆大叔, 757 阅读, 4 评论, 收藏, 编辑 文章内容 通过前面的章节,我们知道HttpApplication在初始化的时 ...

  4. Mvc动态注册HttpModule详解

    序言 注册Httpmodule可以让我们使用HttpApplication对象中的处理管道事件.目前大家所熟知的应该有2种方式来使用HttpApplication对象中的处理管道事件.第一种是通过Gl ...

  5. MVC源码解析 - 配置注册 / 动态注册 HttpModule

    本来这一篇, 是要继续 Pipeline 的, 但是在 Pipeline之前, 我看到了InitModules()方法, 所以决定, 在中间穿插一篇进来. 这一篇来讲一下 IHttpModule 的加 ...

  6. 在Asp.net 4.0 中动态注册HttpModule

    using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...

  7. 动态注册HttpModule管道,实现global.asax功能

    1.所用类库有 Microsoft.Web.Infrastructure.dll 和WebActivator.dll 2.类代码如下 using System; using System.Collec ...

  8. RPC原来就是Socket——RPC框架到dubbo的服务动态注册,服务路由,负载均衡演化

    序:RPC就是使用socket告诉服务端我要调你的哪一个类的哪一个方法然后获得处理的结果.服务注册和路由就是借助第三方存储介质存储服务信息让服务消费者调用.然我们自己动手从0开始写一个rpc功能以及实 ...

  9. Oracle监听的静态注册和动态注册

    静态注册:通过解析listene.ora文件 动态注册:由PMON进程动态注册至监听中 在没有listener.ora配置文件的情况下,如果启动监听,则监听为动态注册.用图形化netca创建的监听,默 ...

随机推荐

  1. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  2. Linux(12.1-12.6)学习笔记

    第十二章 并发编程 如果逻辑控制流在时间上重叠,那么他们就是并发的.应用级并发在以下情况中发挥作用: 访问慢速I/O设备. 与人交互. 通过推迟工作以降低延迟. 服务多个网络客户端. 在多核机器上进行 ...

  3. canvas学习笔记:小小滴公式,大大滴乐趣

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 最近想弄一个网页,把自己学HTML5过程中做的部分DEMO放上去做集合,但是,如果就仅仅做个网页把所有DEMO一个一个排列又觉得太难看了. ...

  4. 【转载】cocs2dx中c++与c#互调

    文章有参考http://www.cnblogs.com/zhxilin/archive/2013/03/20/2971331.html 下面以接入九幽数据统计插件为例 Step 1:如果是cocos2 ...

  5. MVC4/5+jquery+bootstrap样式+dataTables+linq+WCF+EF6后台和前台的框架集合!好蛋疼哦!数据库支持MYSQL 和MSSQL,oracle。集成腾讯企业邮箱收邮件同步用户SSO登陆等功能。

    花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才 ...

  6. SVN技术交流提纲

    SVN技术交流提纲:http://lazio10000.github.io/tech/SVN/#/bored

  7. Bootstrap系列 -- 22. 按钮

    Bootstrap框架首先通过基础类名“.btn”定义了一个基础的按钮风格,然后通过“.btn-default”定义了一个默认的按钮风格.默认按钮的风格就是在基础按钮的风格的基础上修改了按钮的背景颜色 ...

  8. Windows下apache php wordpress配置

    2. Use notepad to open httpd.conf config file. Make use the line "LoadModule rewrite_module mod ...

  9. [USACO2005][POJ2454]Jersey Politics(随机化)

    题目:http://poj.org/problem?id=2454 题意:给你3*k(k<=60)个数,你要将它们分成3个长度为k的序列,使得其中至少有两个序列的和大于k*500 分析:以为有高 ...

  10. Boostrap(1)

    1.简介 Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,可以认为bootstrap就是一个样式库. ...