我的NopCommerce之旅(6): 应用启动

一、基础介绍

Global.asax 文件(也称为 ASP.NET 应用程序文件)是一个可选文件,该文件包含响应 ASP.NET 或 HTTP 模块所引发的应用程序级别和会话级别事件的代码。

Application_Start是其中一个事件,在HttpApplication 类的第一个实例被创建时,该事件被触发它允许你创建可以由所有HttpApplication 实例访问的对象。

简单来说,Application_Start()就是一个ASP.NET应用程序启动时执行的方法,可以理解为应用程序入口。

二、代码分析Application_Start()

1.出于安全考虑,去除X-AspNetMvc-Version头

2.初始化上下文

3.判断是否初始化数据库

  3.1 加载配置文件~/App_Data/Settings.txt

  3.2 读取内容,若存在连接字符串则说明已初始化数据库

4.清空视图引擎,添加自定义视图引擎ThemeableRazorViewEngine,支持前台和后台页面分离,及主题适配。

5.增加一些功能性的元数据

6.注册常见的MVC物件,包括Area,Route

7.关闭MVC默认的标注特性Model验证,添加FluentValidation(一种验证组件)

8.启动定时任务

  8.1 初始化所有创建的定时任务

  8.2 启动定时任务线程

9.根据配置,是否启动MiniProfiler(ASP.NET MVC的性能分析工具,监控网站性能)

  9.1 安装时默认为false,并配置在[dbo].[Setting]表,Name为storeinformationsettings.displayminiprofilerinpublicstore

  9.2 配置方法

    9.2.1 进入管理页面,进入配置菜单

      

    9.2.2 检索storeinformationsettings.displayminiprofilerinpublicstore,定位该条目,修改Value值

      

    9.2.3 启动效果,在页面左上角可看到该页面执行时间,参考MiniProfiler相关资料

      

10.记录应用启动日志

  10.1 通过依赖注入实例化

  10.2 日志写入数据库,表[dbo].[Log]

11.代码如下

 1         protected void Application_Start()
 2         {
 3             //disable "X-AspNetMvc-Version" header name
 4             MvcHandler.DisableMvcResponseHeader = true;
 5 
 6             //initialize engine context
 7             EngineContext.Initialize(false);
 8 
 9             bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled();
10             if (databaseInstalled)
11             {
12                 //remove all view engines
13                 ViewEngines.Engines.Clear();
14                 //except the themeable razor view engine we use
15                 ViewEngines.Engines.Add(new ThemeableRazorViewEngine());
16             }
17 
18             //Add some functionality on top of the default ModelMetadataProvider
19             ModelMetadataProviders.Current = new NopMetadataProvider();
20 
21             //Registering some regular mvc stuff
22             AreaRegistration.RegisterAllAreas();
23             RegisterRoutes(RouteTable.Routes);
24             
25             //fluent validation
26             DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
27             ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new NopValidatorFactory()));
28 
29             //start scheduled tasks
30             if (databaseInstalled)
31             {
32                 TaskManager.Instance.Initialize();
33                 TaskManager.Instance.Start();
34             }
35 
36             //miniprofiler
37             if (databaseInstalled)
38             {
39                 if (EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)
40                 {
41                     GlobalFilters.Filters.Add(new ProfilingActionFilter());
42                 }
43             }
44 
45             //log application start
46             if (databaseInstalled)
47             {
48                 try
49                 {
50                     //log
51                     var logger = EngineContext.Current.Resolve<ILogger>();
52                     logger.Information("Application started", null, null);
53                 }
54                 catch (Exception)
55                 {
56                     //don't throw new exception if occurs
57                 }
58             }
59         }
原文地址:https://www.cnblogs.com/devilsky/p/5359881.html