MVC之前的那点事儿系列(7):WebActivator的实现原理详解
文章内容
上篇文章,我们分析如何动态注册HttpModule的实现,本篇我们来分析一下通过上篇代码原理实现的WebActivator类库,WebActivator提供了3种功能,允许我们分别在HttpApplication初始化之前,之后以及ShutDown的时候分别执行指定的代码,示例如下:
[assembly: WebActivator.PreApplicationStartMethod(typeof(A.InitClass1), "PreStart")]
[assembly: WebActivator.PostApplicationStartMethod(typeof(A.InitClass1), "PostStart")]
[assembly: WebActivator.ApplicationShutdownMethod(typeof(A.InitClass1), "ShutDown")]
另外还有一点和系统自带的PreApplicationStartMethodAttribute不同的是,WebActivator的每种特性都可以使用多次,比如:
[assembly: WebActivator.PreApplicationStartMethod(typeof(A.InitClass1), "PreStart")]
[assembly: WebActivator.PreApplicationStartMethod(typeof(A.InitClass2), "PreStart")]
[assembly: WebActivator.PreApplicationStartMethod(typeof(A.InitClass3), "PreStart")]
因为它的源码很少,所以今天我们就来全面分析一下WebActivator的实现原理,首先下载WebActivator的最新1.5源码,源码地址:https://bitbucket.org/davidebbo/webactivator/src
解压代码,我们可以看到WebActivator项目里总共有6个重要的cs文件,以及一个packages.config文件(用于标记本项目引用了Microsoft.Web.Infrastructure.dll类库),下面我们来分析一下每个文件的源码。
3个XXXMethodAttribute属性:
根据上面的用法,我们指导WebActivator提供了3个MethodAttribute,我们先来看看这3个文件都是如何实现的,查阅代码发现3个类(PreApplicationStartMethodAttribute/ PostApplicationStartMethodAttribute/ ApplicationShutdownMethodAttribute)的内容都是一样的,都是继承于BaseActivationMethodAttribute类,然后提供构造函数所需要的Type类型和方法名称, 3个特性类都支持使用多次并且只能用于Assembly,代码如下:
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
通用的基类BaseActivationMethodAttribute:
using System;
using System.Reflection; namespace WebActivator
{
// Base class of all the activation attributes
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public abstract class BaseActivationMethodAttribute : Attribute
{
private Type _type;
private string _methodName; public BaseActivationMethodAttribute(Type type, string methodName)
{
_type = type;
_methodName = methodName;
} public Type Type { get { return _type; } } public string MethodName { get { return _methodName; } } public int Order { get; set; } public void InvokeMethod()
{
// Get the method
MethodInfo method = Type.GetMethod(
MethodName,
BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); if (method == null)
{
throw new ArgumentException(
String.Format("The type {0} doesn't have a static method named {1}",
Type, MethodName));
} // Invoke it
method.Invoke(null, null);
}
}
}
通过代码,我们首先可以看到,除了Type和MethodName以外,还多了一个Order属性,用来标记多次使用同一个Attribute的时候的执行顺序。然后提供了一个InvokeMethod方法,用来执行该类里传入当前Type类的MethodName静态方法。
Assembly扩展方法AssemblyExtensions:
using System.Collections.Generic;
using System.Linq;
using System.Reflection; namespace WebActivator
{
static class AssemblyExtensions
{
// Return all the attributes of a given type from an assembly
public static IEnumerable<T> GetActivationAttributes<T>(this Assembly assembly) where T : BaseActivationMethodAttribute
{
return assembly.GetCustomAttributes(
typeof(T),
inherit: false).OfType<T>();
}
}
}
该扩展方法主要是用于获取某一个程序集Assembly下指定类型的所有Attribute(并且不包括继承的类),也就是查询上述3种特性的Attributes(因为每种都允许声明多次)。
主管理类ActivationManager:
该类主要分为如下几个部分:
1 私有静态函数Assemblies, GetAssemblyFiles主要是获取当前应用程序下的所有DLL程序集,以供其它方法从这个程序集集合里遍历相应的特性声明。
// 加载所有获取的程序集
private static IEnumerable<Assembly> Assemblies
{
get
{
if (_assemblies == null)
{
// Cache the list of relevant assemblies, since we need it for both Pre and Post
_assemblies = new List<Assembly>();
foreach (var assemblyFile in GetAssemblyFiles())
{
try
{
// Ignore assemblies we can't load. They could be native, etc...
_assemblies.Add(Assembly.LoadFrom(assemblyFile));
}
catch
{
}
}
} return _assemblies;
}
} // 获取程序集文件路径集合
private static IEnumerable<string> GetAssemblyFiles()
{
// When running under ASP.NET, find assemblies in the bin folder.
// Outside of ASP.NET, use whatever folder WebActivator itself is in
string directory = HostingEnvironment.IsHosted
? HttpRuntime.BinDirectory
: Path.GetDirectoryName(typeof(ActivationManager).Assembly.Location);
return Directory.GetFiles(directory, "*.dll");
}
2 获取所有AppCode文件夹下代码编译后的程序集。
// Return all the App_Code assemblies
private static IEnumerable<Assembly> AppCodeAssemblies
{
get
{
// Return an empty list if we;re not hosted or there aren't any
if (!HostingEnvironment.IsHosted || !_hasInited || BuildManager.CodeAssemblies == null)
{
return Enumerable.Empty<Assembly>();
} return BuildManager.CodeAssemblies.OfType<Assembly>();
}
}
3 执行3种特性里所指定的方法
public static void RunPreStartMethods()
{
RunActivationMethods<PreApplicationStartMethodAttribute>();
} public static void RunPostStartMethods()
{
RunActivationMethods<PostApplicationStartMethodAttribute>();
} public static void RunShutdownMethods()
{
RunActivationMethods<ApplicationShutdownMethodAttribute>();
} // Call the relevant activation method from all assemblies
private static void RunActivationMethods<T>() where T : BaseActivationMethodAttribute
{
foreach (var assembly in Assemblies.Concat(AppCodeAssemblies))
{
foreach (BaseActivationMethodAttribute activationAttrib in assembly.GetActivationAttributes<T>().OrderBy(att => att.Order))
{
activationAttrib.InvokeMethod();
}
}
}
从代码可以看出,3个特性执行方法调用的都是同一个泛型方法RunActivationMethods<T>,在这个方法里,主要是从所有的程序集里,通过泛型方法查询所有标记的特性(按Order排序),并且执行每个特性声明里指定的方法。另外从Assemblies.Concat(AppCodeAssemblies)可以发现,所有的程序集还要包括App_Code目录下代码编译的程序集哦。
4 自定义HttpModule
class StartMethodCallingModule : IHttpModule
{
private static object _lock = new object();
private static int _initializedModuleCount; public void Init(HttpApplication context)
{
lock (_lock)
{
// Keep track of the number of modules initialized and
// make sure we only call the post start methods once per app domain
if (_initializedModuleCount++ == )
{
RunPostStartMethods();
}
}
} public void Dispose()
{
lock (_lock)
{
// Call the shutdown methods when the last module is disposed
if (--_initializedModuleCount == )
{
RunShutdownMethods();
}
}
}
}
该Module主要是用于在 Init的时候执行PostStart类型的方法,并且在Dispose的时候执行Shutdown类型的方法,并且只执行一次。
5.最重要的入口方法
public static void Run()
{
if (!_hasInited)
{
RunPreStartMethods(); // Register our module to handle any Post Start methods. But outside of ASP.NET, just run them now
if (HostingEnvironment.IsHosted)
{
Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(StartMethodCallingModule));
}
else
{
RunPostStartMethods();
} _hasInited = true;
}
}
Run方法看起来很容易理解了,首先执行PreStart类型的方法,然后判断HostingEnvironment是否Host成功,如果成功就动态注册我们上面自定义的HttpModule,以便让该Module在HttpApplication初始化和Dispose的时候分别执行PostStart类型的方法和ShutDown类型的方法,如果没有Host成功,那只执行PostStart类型的方法。
注:由代码实现可以看出,在PreStart类型的方法里,不能使用HttpContext对象进行输入输出,因为该对象在此时还没用创建成功呢。
6.谁调用了入口方法Run()
这个就不用多说了吧,肯定是使用.Net4.0自带的PreApplicationStartMethodAttribute特性,代码如下:
[assembly: PreApplicationStartMethod(typeof(WebActivator.ActivationManager), "Run")]
你可以让这段代码放在WebActivator项目里任何类文件的namespace外部,但为了统一起见,一般都是放在Properties目录下的AssemblyInfo类文件里,WebActivator就是这么做的。
总结,好了,这就是WebActivator的全部源码,实现起来其实很简单,对吧?那以后项目再有类似需求的时候,就大胆使用这个类库吧,另外NInject.MVC也是基于这个类库来实现的。
参考资料:
https://bitbucket.org/davidebbo/webactivator/src
同步与推荐
本文已同步至目录索引:MVC之前的那点事儿系列
MVC之前的那点事儿系列文章,包括了原创,翻译,转载等各类型的文章,如果对你有用,请推荐支持一把,给大叔写作的动力。
MVC之前的那点事儿系列(7):WebActivator的实现原理详解的更多相关文章
- MVC之前的那点事儿系列(3):HttpRuntime详解分析(下)
文章内容 话说,经过各种各样复杂的我们不知道的内部处理,非托管代码正式开始调用ISPAIRuntime的ProcessRequest方法了(ISPAIRuntime继承了IISPAIRuntime接口 ...
- MVC之前的那点事儿系列(2):HttpRuntime详解分析(上)
文章内容 从上章文章都知道,asp.net是运行在HttpRuntime里的,但是从CLR如何进入HttpRuntime的,可能大家都不太清晰.本章节就是通过深入分析.Net4的源码来展示其中的重要步 ...
- Spring框架系列(6) - Spring IOC实现原理详解之IOC体系结构设计
在对IoC有了初步的认知后,我们开始对IOC的实现原理进行深入理解.本文将帮助你站在设计者的角度去看IOC最顶层的结构设计.@pdai Spring框架系列(6) - Spring IOC实现原理详解 ...
- Spring框架系列(7) - Spring IOC实现原理详解之IOC初始化流程
上文,我们看了IOC设计要点和设计结构:紧接着这篇,我们可以看下源码的实现了:Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的. ...
- Spring框架系列(8) - Spring IOC实现原理详解之Bean实例化(生命周期,循环依赖等)
上文,我们看了IOC设计要点和设计结构:以及Spring如何实现将资源配置(以xml配置为例)通过加载,解析,生成BeanDefination并注册到IoC容器中的:容器中存放的是Bean的定义即Be ...
- Spring框架系列(9) - Spring AOP实现原理详解之AOP切面的实现
前文,我们分析了Spring IOC的初始化过程和Bean的生命周期等,而Spring AOP也是基于IOC的Bean加载来实现的.本文主要介绍Spring AOP原理解析的切面实现过程(将切面类的所 ...
- Spring框架系列(10) - Spring AOP实现原理详解之AOP代理的创建
上文我们介绍了Spring AOP原理解析的切面实现过程(将切面类的所有切面方法根据使用的注解生成对应Advice,并将Advice连同切入点匹配器和切面类等信息一并封装到Advisor).本文在此基 ...
- Spring框架系列(11) - Spring AOP实现原理详解之Cglib代理实现
我们在前文中已经介绍了SpringAOP的切面实现和创建动态代理的过程,那么动态代理是如何工作的呢?本文主要介绍Cglib动态代理的案例和SpringAOP实现的原理.@pdai Spring框架系列 ...
- Spring框架系列(12) - Spring AOP实现原理详解之JDK代理实现
上文我们学习了SpringAOP Cglib动态代理的实现,本文主要是SpringAOP JDK动态代理的案例和实现部分.@pdai Spring框架系列(12) - Spring AOP实现原理详解 ...
随机推荐
- EF架构~Cannot attach the file as database
回到目录 Cannot attach the file as database这个异常是在EF的code frist里经常出现的,解决方法很简单,只要重新启动一下V11实例即可. CMD> sq ...
- Atitit.加密算法ati Aes的框架设计v2.2
Atitit.加密算法ati Aes的框架设计v2.2 版本进化1 V2.2 add def decode key api1 v1版本1 Aes的历史2 Atitit.加密算法 des aes ...
- paip.java 架构师之路以及java高级技术
paip.java 架构师之路以及java高级技术 1. Annotation 设计模式... 概念满天飞.ORM,IOC,AOP. Validator lambda4j memcache. 对 ...
- hammer.js手势库使用
hammer.js是一款移动端手势库组件,支持pan(拖动).swipe(滑动).tap(轻触).press(按压,即长按).doubletap(双击)等很多手势操作,提供比较完善的事件监听机制,但是 ...
- Microsoft Visual Studio正忙解决办法
问题描述 前段时间用vs2015进行开发.出现如下问题,关闭vs进程重启vs还是无法解决. 如何解决 进入本地项目.vs文件夹 这个文件夹下有个.suo文件,删除该文件,用任务管理器杀掉vs的进程,重 ...
- 哈夫曼树(二)之 C++详解
上一章介绍了哈夫曼树的基本概念,并通过C语言实现了哈夫曼树.本章是哈夫曼树的C++实现. 目录 1. 哈夫曼树的介绍 2. 哈夫曼树的图文解析 3. 哈夫曼树的基本操作 4. 哈夫曼树的完整源码 转载 ...
- PetaPoco 访问SQL SERVER 存储过程
博客园有篇文章<小巧方便的ORM类库——PetaPoco> 介绍了PetaPoco调用存储过程: //调用存储过程 db.Execute("exec procSomeHandl ...
- C/C++:提升_头文件的使用
C/C++:提升_头文件的使用 ◇写在前面 学到现在,很多人编写程序时只会使用一个文件.这样在代码量较小的时候,更利于表达程序,但是随着代码量的逐步增加,程序的思维逻辑不是我们一下子就可以完全理清的, ...
- Android进程间通信之socket通信
用Java中的socket编程. 通过socket实现两个应用之间的通信,可以接收和发送数据,同时将接收到的数据显示在activity界面上. Server端: ServerLastly.java p ...
- ionic+nodejs开发遇到的跨域和post请求数据问题
最近学习ionic+nodejs开发混合app中遇到了一些问题,在此总结一下. 开发环境搭建 项目地址 https://github.com/ytudt/nodejsApp 代码和问题都会在之后的学习 ...