简体   繁体   中英

Application_Start in Global and IProcessHostPreloadClient

I am trying to follow http://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series to implement some start up process in my asp.net application.

As of now we have a quartz.net scheduler registered in ASP.Net application_start method like below and running.

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);
  }
}

Then

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

The problem is, it is stopped running when IIS is recycling application pool. To make it work I tried to follow this approach.

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

In ApplicationHost.config

 <serviceAutoStartProviders>

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

But I read in 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.

Now confused, do I need to have same code in PreWarmUp class as well in Application_Start?

You should have look at Hangfire . It is a great example of how you can do scheduled tasks from an IIS application.

It is a framework to perform fire-and-forget, delayed and recurring tasks, which are manageable from a optional Dashboard.

But also, if you don't use it, you can learn a lot from it's awesome documentation .

You should give your PreWarmUp class in the config:

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

In ApplicationHost.config

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

Then also in the setting of your site, you need to mention to use this service to be used by name. But better do all in script like this one (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

This will not work. You are asking a web application to function like a service. If you have any long running tasks, you will always run up against application pools recycling and if you don't, I'd suspect you turned on an extremely long running application pool setting that will whittle your server's performance away.

My recommendation: stop using a web application for what a windows service was meant to do. Or, write a task scheduler that calls a web page that does what you are doing above on what ever interval you require. This is essentially how lots of PHP applications and cron use to simulate a service via web applications.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM