一、基础介绍

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. OAuth2.0 基础概述

    web:http://oauth.net/2/ rfc:http://tools.ietf.org/html/rfc6749 doc:http://oauth.net/documentation/ c ...

  2. 领域对象模型(domain object model)

    在Play程序中,模型(model)占据了核心地位.它是程序操作的信息的特定领域的表现方式. Martin Fowler这样定义模型: 负责表达业务概念,业务状态信息以及业务规则.尽管保存业务状态的技 ...

  3. apple store链接格式文档

    备份一下: The app on Appstore has specific URL format http://itunes.apple.com/[country-code]/app/[app-na ...

  4. 使用openssl创建自签名证书及部署到IIS教程

    概要 本文讲解三个部分:1. 创建自签名证书2. 创建自己的证书颁发机构3. 以及如何配置IIS 创建自签名证书 首先,创建一个私钥文件: openssl genrsa -out myselfsign ...

  5. Java集合 Json集合之间的转换

    1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...

  6. jQuery源码分析-01总体架构

    1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...

  7. 解决Sharepoint每天第一次打开速度慢的问题

    每天第一次打开Sharepoint的网站会非常慢,下面是解决这个问题的几个方法. 添加crl.microsoft.com到Hosts文件,IP地址指向服务器本机. 允许服务器直接连接到crl.micr ...

  8. 深入理解Activity -动手写实例来感受Activity的启动模式

    介绍 上篇提到了Activity的任务,任务栈,以及启动模式.对这些概念有了了解以后,自己写一下例子来感受一下,就当作复习和加深印象了.如果对概念不熟悉的可以参考:深入理解Activity-任务,回退 ...

  9. 基础学习day12--多线程一线程之间的通信和常用方法

    一.线程之间的通信 1.1.线程之间的通信方法 多个线程在处理统一资源,但是任务却不同,这时候就需要线程间通信.    等待/唤醒机制涉及的方法:    1. wait():让线程处于冻结状态,被wa ...

  10. iOS 完美解决 interactivePopGestureRecognizer 卡住的问题

    interactivePopGestureRecognizer是iOS7推出的解决VeiwController滑动后退的新功能,虽然很实用,但是坑也很多啊,用过的同学肯定知道问题在哪里,所以具体问题我 ...