首页 技术 正文
技术 2022年11月10日
0 收藏 785 点赞 5,066 浏览 6555 个字

一、关于动态注册的问题

  很多人看过汤姆大叔的MVC之前的那点事儿系列(6):动态注册HttpModule

,其实汤姆大叔没有发现httpmodule动态注册的根本机制在哪里.

亦即:怎么动态注册?为什么能够动态注册?

  汤姆大叔给了如下开篇

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

答案是肯定的, ASP.NET MVC3发布的时候提供了一个Microsoft.Web.Infrastructure.dll文件,这个文件就是提供了动态注册HttpModule的功能,那么它是如何以注册的呢?我们先去MVC3的源码看看该DLL的源代码。

  其实httpmodule动态注册,是ASP.NET框架内部自己提供的机制,和MVC没有关系,也就是说有没有MVC,ASP.NET自己都会提供这个机制(没有研究其他

.NET版本,至少在.NET 4.5下是如此的,这是不含MVC框架的情况下)

二、关于httpmodule的初始化

接着前面的章节,我们开始论述,以下面的代码回顾

// System.Web.HttpApplication
internal void InitInternal(HttpContext context, HttpApplicationState state, MethodInfo[] handlers)
{
this._state = state;
PerfCounters.IncrementCounter(AppPerfCounter.PIPELINES);
try
{
try
{
this._initContext = context;
this._initContext.ApplicationInstance = this;
context.ConfigurationPath = context.Request.ApplicationPathObject;
using (new DisposableHttpContextWrapper(context))
{
if (HttpRuntime.UseIntegratedPipeline)
{
try
{
context.HideRequestResponse = true;
this._hideRequestResponse = true;
this.InitIntegratedModules();
goto IL_6B;
}
finally
{
context.HideRequestResponse = false;
this._hideRequestResponse = false;
}
}
this.InitModules();//注意这里,这里是初始化所有的module,其中包括了配置文件中的和动态注册的
IL_6B:
if (handlers != null)
{
this.HookupEventHandlersForApplicationAndModules(handlers);
}
this._context = context;
if (HttpRuntime.UseIntegratedPipeline && this._context != null)
{
this._context.HideRequestResponse = true;
}
this._hideRequestResponse = true;
try
{
this.Init();
}
catch (Exception error)
{
this.RecordError(error);
}
}
if (HttpRuntime.UseIntegratedPipeline && this._context != null)
{
this._context.HideRequestResponse = false;
}
this._hideRequestResponse = false;
this._context = null;
this._resumeStepsWaitCallback = new WaitCallback(this.ResumeStepsWaitCallback);
if (HttpRuntime.UseIntegratedPipeline)
{
this._stepManager = new HttpApplication.PipelineStepManager(this);
}
else
{
this._stepManager = new HttpApplication.ApplicationStepManager(this);
}
this._stepManager.BuildSteps(this._resumeStepsWaitCallback);
}
finally
{
this._initInternalCompleted = true;
context.ConfigurationPath = null;
this._initContext.ApplicationInstance = null;
this._initContext = null;
}
}
catch
{
throw;
}
}
private void InitModules()
{
HttpModulesSection httpModules = RuntimeConfig.GetAppConfig().HttpModules;//配置文件中的
HttpModuleCollection httpModuleCollection = httpModules.CreateModules();//动态注册的
HttpModuleCollection other = this.CreateDynamicModules();
httpModuleCollection.AppendCollection(other);
this._moduleCollection = httpModuleCollection;
this.InitModulesCommon();
}
private HttpModuleCollection CreateDynamicModules()
{
HttpModuleCollection httpModuleCollection = new HttpModuleCollection();
foreach (DynamicModuleRegistryEntry current in HttpApplication._dynamicModuleRegistry.LockAndFetchList())
{
HttpModuleAction httpModuleAction = new HttpModuleAction(current.Name, current.Type);
     //初始化module原来就是在这里
httpModuleCollection.AddModule(httpModuleAction.Entry.ModuleName, httpModuleAction.Entry.Create());
}
return httpModuleCollection;//最终都给了this._moduleCollection
}

可以想象:最后某个地方调用this._moduleCollection就能得到初始化操作.其实就是这里进行各个module初始化的操作的:

private void InitModulesCommon()
{
int count = this._moduleCollection.Count;
for (int i = ; i < count; i++)
{
this._currentModuleCollectionKey = this._moduleCollection.GetKey(i);
this._moduleCollection[i].Init(this);//这里其实就是调用了各个httpmodule的初始化方法
}
this._currentModuleCollectionKey = null;
this.InitAppLevelCulture();
}

但是,这个

this._moduleCollection

集合的数据也是来至于

HttpApplication._dynamicModuleRegistry.LockAndFetchList()

public ICollection<DynamicModuleRegistryEntry> LockAndFetchList(),如果这里的数据中加入我们动态注册的module,

那么就达到了动态注册的目的

public ICollection<DynamicModuleRegistryEntry> LockAndFetchList()
{
ICollection<DynamicModuleRegistryEntry> entries;
lock (this._lockObj)
{
this._entriesReadonly = true;
entries = this._entries;//即:我们往这个里面_entries加入我们需要的module就达到效果
}
return entries;
}

恰巧在httpapplication中,有一个注册module的方法,估计很多人都没有用过

public static void RegisterModule(Type moduleType)
{
RuntimeConfig appConfig = RuntimeConfig.GetAppConfig();
HttpRuntimeSection httpRuntime = appConfig.HttpRuntime;
if (httpRuntime.AllowDynamicModuleRegistration)
{
HttpApplication.RegisterModuleInternal(moduleType);
return;
}
throw new InvalidOperationException(SR.GetString("DynamicModuleRegistrationOff"));
}
internal static void RegisterModuleInternal(Type moduleType)
{
HttpApplication._dynamicModuleRegistry.Add(moduleType);//从这里,我们可以看出该方法恰巧就可以动态注册.
}
public void Add(Type moduleType)
{
if (moduleType == null)
{
throw new ArgumentNullException("moduleType");
}
if (!typeof(IHttpModule).IsAssignableFrom(moduleType))
{
string message = string.Format(CultureInfo.CurrentCulture, SR.GetString("DynamicModuleRegistry_TypeIsNotIHttpModule"), new object[]
{
moduleType
});
throw new ArgumentException(message, "moduleType");
}
lock (this._lockObj)
{
if (this._entriesReadonly)
{
throw new InvalidOperationException(SR.GetString("DynamicModuleRegistry_ModulesAlreadyInitialized"));
}
this._entries.Add(new DynamicModuleRegistryEntry(DynamicModuleRegistry.MakeUniqueModuleName(moduleType), moduleType.AssemblyQualifiedName));
}
}

恰巧其机制就重合了,也就是动态注册的效果来源于这里.

this._entries.Add

但我们实际中会发现,我们如果纯粹地调用httpapplication的RegisterModule方法是达不到目的,例如我们在Global文件中加入以下代码

public Global()
{
//DynamicModuleRegistry
HttpApplication.RegisterModule(typeof(CustomModule));
InitializeComponent(); }

系统抛出异常:

行 26: {
行 27: //DynamicModuleRegistry
行 28: HttpApplication.RegisterModule(typeof(CustomModule));
行 29: InitializeComponent();
行 30:

源文件: c:\Users\qscq\Documents\SharpDevelop Projects\ASPNET_ST_1\ASPNET_ST_1\Global.asax.cs    行: 28

  这又是为什么呢?

  其实异常的来源在于

在 System.Web.DynamicModuleRegistry.Add(Type moduleType)

public void Add(Type moduleType)
{
if (moduleType == null)
{
throw new ArgumentNullException("moduleType");
}
if (!typeof(IHttpModule).IsAssignableFrom(moduleType))
{
string message = string.Format(CultureInfo.CurrentCulture, SR.GetString("DynamicModuleRegistry_TypeIsNotIHttpModule"), new object[]
{
moduleType
});
throw new ArgumentException(message, "moduleType");
}
lock (this._lockObj)
{
if (this._entriesReadonly)
{
throw new InvalidOperationException(SR.GetString("DynamicModuleRegistry_ModulesAlreadyInitialized"));
}
this._entries.Add(new DynamicModuleRegistryEntry(DynamicModuleRegistry.MakeUniqueModuleName(moduleType), moduleType.AssemblyQualifiedName));
}
}

  也就是说,_entriesReadonly此刻已经是true了, 也就是在其true之前加入module就没有问题了.那什么时候变成true的呢?其实就是前面讲到的httpapplication自己初始化的时候,即InitModules方法中.也就是说,我们要抢在系统调用该方法前,调用

HttpApplication.RegisterModule方法就可以了.

三、总结

  如上面说的那样,系统自己调用了HttpApplication.RegisterModule,在调用此方法前,我们能够动态注册module即可.

汤姆大叔给出了

[assembly: PreApplicationStartMethod(typeof(WebApplication1.Test.PreApplicationStartCode), "PreStart")]

  该方法显然可以达到目标.但我们可以直接来得更简单些,直接Global加一个静态构造函数

  即:

static   Global(){
HttpApplication.RegisterModule(typeof(CustomModule));}

  

四、感谢

  感谢几日来大家关注,我会继续写好,兄台,求推荐、关注.

大赠送的东西,大家可以好好看看,算是回馈大家的支持

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290