c# - Global 和 IProcessHostPreloadClient 中的 Application_Start

标签 c# asp.net asp.net-mvc iis quartz.net

我正在尝试关注http://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series在我的 ASP.NET 应用程序中实现一些启动过程。

到目前为止,我们已经在 ASP.Net application_start 方法中注册了一个quartz.net 调度程序,如下所示并正在运行。

public static class QuartzHelper
{
 public static void RunJob()
 {
  ISchedulerFactory schedFact = new StdSchedulerFactory();
    IScheduler sched = schedFact.GetScheduler();
    sched.Start();
    IJobDetail job = JobBuilder.Create<SendDailyMorningSMSJob>()
    .WithIdentity("Auto_DailyMorning8AM", "AutoSMS")
    .Build();
    ITrigger trigger = TriggerBuilder.Create()
    .WithIdentity("SMS_Trigger", "AutoSMS")
    .WithSchedule(CronScheduleBuilder
    .DailyAtHourAndMinute(08,00)
    .InTimeZone(TimeZoneInfo.FindSystemTimeZoneById("Central European Standard Time")))
    .Build();
     sched.ScheduleJob(job, trigger);
  }
}

然后

protected void Application_Start()
{
   QuartzHelper.RunJob();
}

问题是,当IIS回收应用程序池时,它停止运行。为了使其发挥作用,我尝试遵循这种方法。

public class PreWarmUp : IProcessHostPreloadClient
{
  public void Preload(string[] parameters)
  {
    QuartzHelper.RunJob();
  }
}

在ApplicationHost.config中

 <serviceAutoStartProviders>

  <add
    name="MCVApplicationNamespace"
    type="MCVApplicationNamespace.QuartzHelper, QuartzHelper" />
 </serviceAutoStartProviders>

但是我读到了MSDN

This interface is intended primarily for use by WCF applications that are non-HTTP applications. ASP.NET developers who want to preload ASP.NET Web applications should use the simulated HTTP requests in IIS 7.0 combined with the Application_Start method in the Global.asax file.

现在很困惑,我是否需要在 PreWarmUp 类中以及 Application_Start 中使用相同的代码?

最佳答案

您应该在配置中提供您的 PreWarmUp 类:

public class PreWarmUp : IProcessHostPreloadClient
{
  public void Preload(string[] parameters)
  {
    QuartzHelper.RunJob();
  }
}

在ApplicationHost.config中

 <serviceAutoStartProviders>
  <add name="PreWarmUp"
    type="NameSpace1.WhateverYourNameSpace.PreWarmUp, AssemblyName1" />
 </serviceAutoStartProviders>

然后,在您网站的设置中,您还需要提及使用此服务以按名称使用。但最好在像这样的脚本中完成所有操作(Powershell):

Add-PSSnapin WebAdministration -ErrorAction SilentlyContinue
Import-Module WebAdministration -ErrorAction SilentlyContinue

$ApplicationPreloadType = "NameSpace1.WhateverYourNameSpace.PreWarmUp, AssemblyName1"
$ApplicationPreloadProvider = "PreWarmUp"
$WebSiteFullName = "IIS:\Sites\myWebApp"

Set-WebConfiguration -Filter '/system.applicationHost/serviceAutoStartProviders' -Value (@{name=$ApplicationPreloadProvider;type=$ApplicationPreloadType})
Set-ItemProperty $WebSiteFullName -Name applicationDefaults.serviceAutoStartEnabled -Value True
Set-ItemProperty $WebSiteFullName -Name applicationDefaults.serviceAutoStartProvider -Value $ApplicationPreloadProvider

关于c# - Global 和 IProcessHostPreloadClient 中的 Application_Start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25325323/

相关文章:

asp.net - 缺少 IIS 10.0 上的 asp.net 功能

asp.net - 缺少程序集 : WindowsBase, PresentationCore、PresentationFramework

javascript - 动态更改应用程序中不同模块的java脚本和css文件

c# - 使用 HTMLTextWriter 呈现服务器端控件会使控件失去标准功能(即单击事件处理程序)

c# - 使用 C# WinForms 在 LocalMachine 注册表字段中创建 DWORD 时遇到问题

c# - 如何为在线资源生成唯一的文本 ID

asp.net - 如何在 ASP.NET 响应中将传输编码显式或隐式设置为分块?

c# - "this"作为参数是引用吗?

c# - 在 C# 中异步检索 Cpu 使用情况

html - 向我的验证消息 ASP.Net MVC 添加换行符