不使用配置文件动态注册HttpModule
在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的更多相关文章
- 你必须知道ASP.NET知识------关于动态注册httpmodule(对不起汤姆大叔)
一.关于动态注册的问题 很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule ,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里. 亦即:怎么动态注册?为什 ...
- MVC之前的那点事儿系列(6):动态注册HttpModule
文章内容 通过前面的章节,我们知道HttpApplication在初始化的时候会初始化所有配置文件里注册的HttpModules,那么有一个疑问,能否初始化之前动态加载HttpModule,而不是只从 ...
- 动态注册HttpModule
动态注册HttpModule 2014-06-05 08:58 by 汤姆大叔, 757 阅读, 4 评论, 收藏, 编辑 文章内容 通过前面的章节,我们知道HttpApplication在初始化的时 ...
- Mvc动态注册HttpModule详解
序言 注册Httpmodule可以让我们使用HttpApplication对象中的处理管道事件.目前大家所熟知的应该有2种方式来使用HttpApplication对象中的处理管道事件.第一种是通过Gl ...
- MVC源码解析 - 配置注册 / 动态注册 HttpModule
本来这一篇, 是要继续 Pipeline 的, 但是在 Pipeline之前, 我看到了InitModules()方法, 所以决定, 在中间穿插一篇进来. 这一篇来讲一下 IHttpModule 的加 ...
- 在Asp.net 4.0 中动态注册HttpModule
using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { publ ...
- 动态注册HttpModule管道,实现global.asax功能
1.所用类库有 Microsoft.Web.Infrastructure.dll 和WebActivator.dll 2.类代码如下 using System; using System.Collec ...
- RPC原来就是Socket——RPC框架到dubbo的服务动态注册,服务路由,负载均衡演化
序:RPC就是使用socket告诉服务端我要调你的哪一个类的哪一个方法然后获得处理的结果.服务注册和路由就是借助第三方存储介质存储服务信息让服务消费者调用.然我们自己动手从0开始写一个rpc功能以及实 ...
- Oracle监听的静态注册和动态注册
静态注册:通过解析listene.ora文件 动态注册:由PMON进程动态注册至监听中 在没有listener.ora配置文件的情况下,如果启动监听,则监听为动态注册.用图形化netca创建的监听,默 ...
随机推荐
- Asp.net通过Jquery操作WebService进行Ajax读写
一说到开始,我们当然需要项目. 首先,创建一个Asp.net Web 应用,然后新增一个名称为“Web 服务”的文件,也就是后缀名为".asmx"的文件,然后需要进行配置一下,在W ...
- win7桌面移到其他盘
打开"计算机",点"用户"--"Administrator"点进去,或者,打开我的文档,然后右键点"桌面""属 ...
- Linux基础入门学习笔记20135227黄晓妍
学习计时:共24小时 读书:1小时 代码:8小时 作业:3小时 博客:12小时 一.学习目标 1. 能够独立安装Linux操作系统 2. 能够熟练使用Linux系统的基本命令 3. 熟练使用L ...
- 从Lumia退役看为什么WP走向没落
前段时间决定将自己用了三年多的Lumia 800正式退役,这是我用的时间最长的手机,虽然系统上有缺陷,但是好不妨碍他成为我最有感情的一部手机.由于之前是WinPhone 开发者的关系,这部手机是微软送 ...
- xsd、wsdl生成C#类的命令行工具使用方法
1.xsd生成C#类命令 示例:xsd <xsd文件路径> /c /o:<生成CS文件目录> <其他参数> 参数说明: /c 生成为cs文件,/d 生成DataSe ...
- 写在读ng之前的基础知识----笔记
如果要看angular的代码, 先把这个给看了, 司徒的干货. /******************************************************************* ...
- iOS开发获取本机手机号码
最近有个奇葩需求,用户登录返回手机号匹配本机号码相同才可以登录,吓得我虎躯一震,经了解,iOS7后不越狱实现不了 "For security reasons, iPhone OS restr ...
- Java-set
set public interface Set<E> extends Collection<E> 使用集合汇总 package 集合类.Set类; /** * Set不允许重 ...
- 【HDU 5578】Friendship of Frog
题 题意 求相同字母最近距离 分析 用数组保存各个字母最后出现的位置,维护最小距离. 代码 #include <cstdio> int c[30],n,p,a,minl; char ch; ...
- HDU1907 John
Description Little John is playing very funny game with his younger brother. There is one big box fi ...