一、基础介绍

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.代码如下

         protected void Application_Start()
{
//disable "X-AspNetMvc-Version" header name
MvcHandler.DisableMvcResponseHeader = true; //initialize engine context
EngineContext.Initialize(false); bool databaseInstalled = DataSettingsHelper.DatabaseIsInstalled();
if (databaseInstalled)
{
//remove all view engines
ViewEngines.Engines.Clear();
//except the themeable razor view engine we use
ViewEngines.Engines.Add(new ThemeableRazorViewEngine());
} //Add some functionality on top of the default ModelMetadataProvider
ModelMetadataProviders.Current = new NopMetadataProvider(); //Registering some regular mvc stuff
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes); //fluent validation
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false;
ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new NopValidatorFactory())); //start scheduled tasks
if (databaseInstalled)
{
TaskManager.Instance.Initialize();
TaskManager.Instance.Start();
} //miniprofiler
if (databaseInstalled)
{
if (EngineContext.Current.Resolve<StoreInformationSettings>().DisplayMiniProfilerInPublicStore)
{
GlobalFilters.Filters.Add(new ProfilingActionFilter());
}
} //log application start
if (databaseInstalled)
{
try
{
//log
var logger = EngineContext.Current.Resolve<ILogger>();
logger.Information("Application started", null, null);
}
catch (Exception)
{
//don't throw new exception if occurs
}
}
}

我的NopCommerce之旅(6): 应用启动的更多相关文章

  1. [转]NopCommerce之旅: 应用启动

    本文转自:http://www.cnblogs.com/devilsky/p/5359881.html 我的NopCommerce之旅(6): 应用启动   一.基础介绍 Global.asax 文件 ...

  2. 我的NopCommerce之旅(8): 路由分析

    一.导图和基础介绍 本文主要介绍NopCommerce的路由机制,网上有一篇不错的文章,有兴趣的可以看看NopCommerce源码架构详解--对seo友好Url的路由机制实现源码分析 SEO,Sear ...

  3. 我的NopCommerce之旅(7): 依赖注入(IOC/DI)

    一.基础介绍 依赖注入,Dependency Injection,权威解释及说明请自己查阅资料. 这里简单说一下常见使用:在mvc的controller的构造方法中定义参数,如ICountryServ ...

  4. 我的NopCommerce之旅(9): 编写Plugin实例

    一.基础介绍 ——In computing, a plug-in (or plugin) is a set of software components that add specific abili ...

  5. 我的NopCommerce之旅(5): 缓存

    一.基础介绍 1.什么是cache      Web缓存是指一个Web资源(如html页面,图片,js,数据等)存在于Web服务器和客户端(浏览器)之间的副本. 2.为什么要用cache      即 ...

  6. 我的NopCommerce之旅(4): 定时任务之邮件

    一.功能简介 用户购买物品生成订单后,系统将发送邮件提醒给用户 二.操作步骤 后台配置一个系统的默认发送邮箱 启动定时任务,这里包括多个任务,只需要启动邮件任务 查看邮件发送情况 三.数据库分析 [d ...

  7. 我的NopCommerce之旅(1): 系统综述

    一.概述 NopCommerce是一个开源的购物网站,它的特点是Pluggable modular/layered architecture(可插拔模块分层架构) 二.功能特色介绍 1.适配手机端 2 ...

  8. 我的NopCommerce之旅(3): 系统代码结构分析

    一.概述 基于MVC 二.详细描述 \Libraries\Nop.Core 核心类,包括缓存.事件.帮助类.业务对象(订单.客户实体) \Libraries\Nop.Data 数据访问层,采用Enti ...

  9. 我的NopCommerce之旅(2): 系统环境及技术分析

    一.系统环境 IIS7.0 or above ASP.NET 4.5(MVC 5.0) .NET Framework 4.5.1 or above VS 2012 or above 二.架构设计 Pl ...

随机推荐

  1. MarkDown学习记录

    一.基本语法 1.标题 建议在#后面加一个空格 2.列表 注意:符号和文字之间加上一个字符的空格 3.引用 4.链接 5.图片 6.粗体与斜体 7.代码框 8.分隔符 二.参考文章 http://ww ...

  2. EntityFramework left join

       var result = from u in db.Order                              join n in db.Equipment on u.OrderId  ...

  3. jython 2.7 b3发布

    Jython 2.7b3 Bugs Fixed - [ 2108 ] Cannot set attribute to instances of AST/PythonTree (blocks pyfla ...

  4. 记录php日志

    1.记录PHP错误日志 display_errors与log_errors的区别 display_errors 错误回显,一般常用于开发模式,但是很多应用在正式环境中也忘记了关闭此选项.错误回显可以暴 ...

  5. Powerbuilder编写身份证校验码

    public function boolean of_calc_cardid_verifycode (string as_cardid, ref string as_verifycode); /* 计 ...

  6. 使用svcutil.exe 工具来生成调用文件

    svcutil.exe http://localhost:9065/ServiceDemo.svc?wsdl 这将生成一个配置文件和一个包含客户端类的代码文件. 下面我们就用这个是怎么生成的: 1,打 ...

  7. AngularJS的指令用法

    scope的绑定策略: @ :把当前属性作为字符串传递,你还可以绑定来自外层scope的值,在属性值中插入 {{}}即可 示例代码: scopeAt.html <!doctype html> ...

  8. 简洁侧边wordpress博客模板

    模板描述:商务领航,尽现成熟稳重的个人小站风格     响应式Web设计,自适应电脑.平板电脑.移动设备     图标字体库,自适应各种终端设备,保证图形图标清晰无锯齿,支持Retina(视网膜屏幕) ...

  9. 汉化入门之ExplorerControls

    第一次汉化,高手勿喷. 01.问题描述 在ArcGIS中有个添加数据窗口,如果在应用程序中直接调用它,则风格一致性则存在问题,很多时间我们都自定义添加数据窗口,我曾经也尝试过.详见ExplorerCo ...

  10. SharePoint 2010 文档管理系列之星级评论功能

    前言:正如我们前面介绍的是,文档管理就是让大家更加直观.方便的对手里的文档,进行统筹掌控,哪些文档是有价值的,哪些文档更受大家欢迎,所有就带来了这个星级评论. 当然,这个是SharePoint 201 ...