2

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?

Billa
  • 5,226
  • 23
  • 61
  • 105

3 Answers3

2

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
Baris
  • 668
  • 1
  • 6
  • 7
1

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.

-1

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.

Josh
  • 10,352
  • 12
  • 58
  • 109
  • 1
    OOPS. Then, why they created a famous component like http://www.quartz-scheduler.net/ – Billa Aug 15 '14 at 14:45
  • Because it was meant for jobs running inside standalone apps, not web applications. Look at all the examples. They are all winforms or console applications, not web applications. – Josh Aug 15 '14 at 15:14
  • http://stackoverflow.com/questions/1356789/how-to-use-quartz-net-with-asp-net Just take a look – Billa Aug 18 '14 at 09:59