实现 Application_Start 和 Application_End
理解 ASP.NET Core: 实现 Application_Start 和 Application_End
在 ASP.NET 中两个常用的处理节点是 Application_Start() 和 Application_End(),它是在第一个请求到达网站的时候被执行和网站停止服务的时候被执行。通常用来进行网站的初始化处理和网站的扫尾处理。
在 ASP.NET Core 中没有了默认的请求处理管道,也没有了 Global.asax,怎样处理这两个事件呢?
在中间件中并不方便处理,中间件将在每个请求到达服务器的时候处理。
这需要通过 ASP.NET Core 的应用程序生命周期来管理。
在 ASP.NET Core 中,网站作为 Host 中寄宿的一个服务,服务使用一个生命周期管理对象 IApplicationLifetime 暴露其生命周期。该对象提供了 3 个重要的事件。我们可以在需要的事件上注册回调函数。
/// <summary>
/// Triggered when the application host has fully started and is about to wait
/// for a graceful shutdown.
/// </summary>
CancellationToken ApplicationStarted { get; }
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// Requests may still be in flight. Shutdown will block until this event completes.
/// </summary>
CancellationToken ApplicationStopping { get; }
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// All requests should be complete at this point. Shutdown will block
/// until this event completes.
/// </summary>
CancellationToken ApplicationStopped { get; }
在 GitHub 中查看 IApplicationLifetime 源码
IApplicationLifetime 同样可以通过注入而使用,例如,我们可以在 Configure() 方法中注入并使用该对象。
public void Configure(IApplicationBuilder app,
Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime,
ILoggerFactory loggerFactory)
{
// use applicationLifetime
}
使用注入的对象注册,这里注册了服务停止事件。
public class Startup
{
private ILogger _logger;
public void Configure(
IApplicationBuilder app,
Microsoft.Extensions.Hosting.IHostApplicationLifetime applicationLifetime,
ILoggerFactory loggerFactory)
{
applicationLifetime.ApplicationStarted.Register(OnStartup);
applicationLifetime.ApplicationStopping.Register(OnShutdown);
...
// add logger providers
// loggerFactory.AddConsole()
...
_logger = loggerFactory.CreateLogger("StartupLogger");
}
private void OnStartup()
{
}
private void OnShutdown()
{
// use _logger here;
}
}
需要注意的是,这里的事件没有使用 EventHandler,而是使用了 CancellationToken,原因是 CancellationToken 支持在多线程情况下使用。
实现 Application_Start 和 Application_End的更多相关文章
- Application_Start和Application_End事件执行时间
Application_start: 第一个访问网站的用户会触发该方法. 通常会在该方法里定义一些系统变量,如聊天室的在线总人数统计,历史访问人数统计的初始化等等均可在这里定义. Applicatio ...
- SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)
在一个网站启动.结束时,我们经常有些操作是需要执行的. 熟悉Asp.net的朋友,使用Global.asax很容易就搞定,在其中有Application_Start和Application_E ...
- 实时数据显示--SignalR实例演示
近段时间,有实现一个看板的功能,就是用户更新信息时,即是对数据库的数据进行插入,更新,或是删除时,在墙上的屏幕的数据不需要人为去刷新,用户就能看到更新后的数据. 实现此功能,Insus.NET使用Si ...
- When using SqlDependency without providing an options value, SqlDependency.Start() must be called prior to execution of a command added to the SqlDependency instance.
在调试SignalR程序时,提示一个异常:When using SqlDependency without providing an options value, SqlDependency.Star ...
- ASP.NET Global.asax详解
最近在研究bbsmax的代码,但是一直不知道入口在哪里,然后就对各个文件分析了,然后终于在对global.asax文件查看的时候看到Application_BeginRequest才明白入口,所以现在 ...
- Asp.Net(C#)自动执行计划任务的程序实例分析
在业务复杂的应用程序中,有时候会要求一个或者多个任务在一定的时间或者一定的时间间隔内计划进行,比如定时备份或同步数据库,定时发送电子邮件等,我们称之为计划任务.实现计划任务的方法也有很多,可以采用SQ ...
- 关于ASP.NET与CLR相互关系的一些总结
原文地址:http://www.cnblogs.com/jasenkin/archive/2010/10/20/asp-net-clr-relation.html CLR(COM服务器) CLR作为一 ...
- ASP.NET页面与IIS底层交互和工作原理详解
转载自:http://www.cnblogs.com/lidabo/archive/2012/03/13/2393200.html 第一回: 引言 我查阅过不少Asp.Net的书籍,发现大多数作者都是 ...
- HttpModule和Http Handler (比较与区别)
HttpModule和Http Handler (比较与区别) HttpModule概述 暂时先不考虑我们自己实现Http Module的情况.在.Net中,Http Module 是实现了IHttp ...
随机推荐
- Error in mounted hook: "TypeError: handlers[i].call is not a function" 原因
Error in mounted hook: "TypeError: handlers[i].call is not a function" 百度翻译 安装钩子中的错误:" ...
- ECC ~ Edge-Conditioned Filter in CNN on Graphs
ECC的卷积操作和常规的二维图像卷积操作都是一种加权平均操作,不同之处在于ECC可以作用在任何图结构上,并且其权重由节点间的边权所决定. 考虑$G=(V,E)$, 其中$|V|=n$ 边 $E \in ...
- Boxing
测试自动装箱和自动拆箱,意思是运行的时候编译器帮我们加了两个代码: public class AutoBoxingandUnBoxing { public static void main(Strin ...
- 串口服务器和modbus网关有什么不同
串口服务器是什么? 串口服务器一般也会被称之为串口设备服务器,它是一种小型电子设备,可以将以太网IP/TCP数据包转换为RS232,RS485或RS422串口数据信号,反之亦然. Modbus网关是什 ...
- Java学习的第四十二天
1.例4.7弦截法求方程f(x)=x^3-5x^2+16x-80=0的根 import java.util.Scanner; import java.lang.*; public class cjav ...
- 深入IOC及其启动原理
IOC总结 1. IOC概述 三个问题: IOC是什么 为什么用它 怎么用 1.1 是什么? 两个概念:控制反转,依赖注入 来看一下传统的干活方式:在对象单一职责原则的基础上,一个对象很少有不依赖其他 ...
- Newton插值的C++实现
Newton(牛顿)插值法具有递推性,这决定其性能要好于Lagrange(拉格朗日)插值法.其重点在于差商(Divided Difference)表的求解. 步骤1. 求解差商表,这里采用非递归法(看 ...
- SQL Server 存储过程解析XML传参 参考方案
1.定义存储过程 -- =============================================--定义存储过程-- ================================ ...
- 八位“Booth二位乘算法”乘法器
目录 八位"Booth二位乘算法"乘法器 原理 补码乘法器 Booth一位乘 Booth二位乘 设计思路 减法变加法 vivado特性 设计文件 综合电路 测试文件 仿真波形 八位 ...
- 性能工具-io工具
I/O:某网上问题通过top iotop pidstat vmstat 工具定位出io高原因,内存不够.