从 Niject Loader 看到过 , [assembly: System.Web.PreApplicationStartMethod ….]
这次再看一下 kooboo , 也同样通过这样的方式,启动代码方式,有意思,
先是注册启动方法
[assembly: System.Web.PreApplicationStartMethod(typeof(Kooboo.CMS.Sites.Providers.SqlServer.AssemblyInitializer), "Initialize")]
namespace Kooboo.CMS.Sites.Providers.SqlServer
{
public static class AssemblyInitializer
{
public static void Initialize()
{
ApplicationInitialization.RegisterInitializerMethod(delegate()
{
ResetProviders();
}, 1);
}
//.......
}
}
然后在 Global.asax ,
protected void Application_Start()
{
//execute the initializer method.
ApplicationInitialization.Execute();
// .....
}
namespace Kooboo
{
public static class ApplicationInitialization
{
private class InitializationItem
{
public Action InitializeMethod { get; set; }
public int Priority { get; set; }
}
private static List<InitializationItem> items = new List<InitializationItem>();
public static void RegisterInitializerMethod(Action method, int priority)
{
items.Add(new InitializationItem() { InitializeMethod = method, Priority = priority });
}
public static void Execute()
{
lock (items)
{
foreach (var item in items.OrderBy(it => it.Priority))
{
item.InitializeMethod();
}
items.Clear();
}
}
}
}