不使用配置文件动态注册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创建的监听,默 ...
随机推荐
- json跨域原理及解决方法
这一篇文章呢,主要是之前一直听别人讲json跨域跨域,但是还是一头雾水,只知其一,于是一怒之下,翻阅各种资料,如果有不正确的地方,劳烦指正一下^_^ 首先,先了解浏览器有一个很重要安全性限制,即为同源 ...
- 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(二)菜单导航
一.前言 上篇博客中已经总体的说了一下权限系统的思路和表结构设计,那接下来我们就要进入正文了,先从菜单导航这个功能开始. 二.实现 这个页面基本不用什么需求分析了,大家都很明白,不过在这个页面要多维护 ...
- 一起用HTML5 canvas做一个简单又骚气的粒子引擎
前言 好吧,说是"粒子引擎"还是大言不惭而标题党了,离真正的粒子引擎还有点远.废话少说,先看demo 本文将教会你做一个简单的canvas粒子制造器(下称引擎). 世界观 这个简单 ...
- javascript 事件传播与事件冒泡,W3C事件模型
说实话笔者在才工作的时候就听说了什么"事件冒泡",弄了很久才弄个大概,当时理解意思是子级dom元素和父级dom元素都绑定了相同类型的事件,这时如果子级事件触发了父级也会触发,然后这 ...
- 多个相同name的文本输入框,输入其中一个后,使剩下的不能输入值
可以用blur或keyup事件响应: 实现一: <body> <input type="text" id="AfterOtOt1" name= ...
- 第四十课:CSS3 transition详解
W3C中对transition是这样描述的:允许css的属性值在一定的时间内平滑的过渡,也就是说,以动画的效果改变css的属性值. transition主要包含4个属性值:transition-pro ...
- POJ 2153 stl
#include<iostream> #include<map> #include<string> using namespace std; int main() ...
- [Asp.net mvc] 在Asp.net mvc 中使用MiniProfiler
MiniProfiler是Stack Overflow团队设计的一款性能分析的小程序.可以对一个页面本身,及该页面通过直接引用.Ajax.Iframe形式访问的其它页面进行监控,监控内容包括数据库内容 ...
- 【转】从Go、Swift语言出发
Google于2009年第一次提出了Go的构思,Facebook在去年春天引入了Hack,随后不久Apple也发布了其Swift语言. 在战争中,胜利者写历史书:在科技中,赢的公司都在写编程语言.互联 ...
- BZOJ-1013 球形空间产生器sphere 高斯消元+数论推公式
1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3662 Solved: 1910 [Subm ...