【发布时间】:2018-04-07 19:28:54
【问题描述】:
有人知道使用 ASP.NET 方法 Application_Start 进行应用程序预热有什么反对意见吗?
特别是对于网络服务,通常需要在哪里预加载文件、缓存算法等。
存在:
- Service Auto Start Providers, serviceAutoStartProvider, https://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series ,我们需要的地方:
- 注册新的
serviceAutoStartProvider,使用具体的程序集名称。 - 将
serviceAutoStartProvider分配给 IIS 应用程序。 - 将 IIS 应用程序配置为 AlwaysRunning。
- 注册新的
- IIS 8.0 应用程序初始化,https://docs.microsoft.com/en-us/iis/get-started/whats-new-in-iis-8/iis-80-application-initialization
- 将
initializationPage分配给 IIS 应用程序。 - 将 IIS 应用程序配置为 AlwaysRunning。
- 将
但是,这两种方法都需要更改每个应用程序的 IIS 配置,这在多对多应用程序的情况下会让人不舒服,并且在发布时会带来额外的风险。
按照ASP.NET Application Life Cycle Overview for IIS 5.0 and 6.0:
ASP.NET 在应用程序域的生命周期内调用它们(Application_Start 和 Application_End)一次,而不是针对每个 HttpApplication 实例。
因此,Application_Start 似乎是热身代码的好地方,例如:
protected void Application_Start(object sender, EventArgs e)
{
Task.Run(WarmUpBackend);
}
只需将 IIS 应用程序配置为 AlwaysRunning。在WarmUpBackend 中,我们可以预加载所有我们需要的网络服务。
【问题讨论】: