在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. Tomcat7.x 与 Tomcat6.x

    试用 Tomcat7.x 与 Tomcat6.x 的明显不同 + Context 填写方法 + 默认应用配置方法 标签: tomcat数据库驱动程序数据库虚拟机jdbcjavascript 2012- ...

  2. 免费Flash图表工具FusionChart

    图表显示是很多开发工作所必不可少的一项功能,今天我介绍一个前段时间发现的免费的Flash图表开发工具,可以通过Adobe Flash实现数据的图表化,动态化以及相互交互. FusionChart是一个 ...

  3. Linux基础与Linux下C语言编程基础

    Linux基础 1 Linux命令 如果使用GUI,Linux和Windows没有什么区别.Linux学习应用的一个特点是通过命令行进行使用. 登录Linux后,我们就可以在#或$符后面去输入命令,有 ...

  4. 系统级I/O

    Unix I/O 输入操作是从I/O设备拷贝数据到主存,而输出操作是从主存拷贝数据到I/O设备. 一个文件就是一个字节序列. 所有的I/O设备,如网络.磁盘.和终端,都被模型化为文件,而所有的输入和输 ...

  5. 20145208 实验四 Android开发基础

    20145208 实验四 Android开发基础 安装Android Studio 安装的具体步骤在老师的链接中已经很详细了,在此就不做赘述了. 在此提出我觉得安装的时候需要注意的两个地方 一是安装地 ...

  6. 小白学习mysql之索引初步

    导语 索引在数据库中的地位是及其的重要,同时要想完全的掌握索引并不是一件容易的事,需要对数据的查询原理以及计算机操作系统有深刻的认识,当然相关的算法和数据结构也是必须的.因此,这篇文章感到了一些压力, ...

  7. ASP.NET 系列:RBAC权限设计

    权限系统的组成通常包括RBAC模型.权限验证.权限管理以及界面访问控制.现有的一些权限系统分析通常存在以下问题: (1)没有权限的设计思路 认为所有系统都可以使用一套基于Table设计的权限系统.事实 ...

  8. 在C#中使用官方驱动操作MongoDB ---转载

    http://blog.csdn.net/dannywj1371/article/details/7440916

  9. jQuery UI dialog

    初始化参数 对于 dialog 来说,首先需要进行初始化,在调用 dialog 函数的时候,如果没有传递参数,或者传递了一个对象,那么就表示在初始化一个对话框. 没有参数,表示按照默认的设置初始化对话 ...

  10. [codevs 1051]接龙游戏(栈)

    题目:http://codevs.cn/problem/1051/ 分析: 当然单词查找树是可以的,但这题有更为简便的方法.可以先按字典序排序,然后弄一个栈,如果当前字串可以接到栈顶元素的后面,那么当 ...