youen
youen

Reputation: 2002

asp.net application does not auto-start

I'm trying to achieve the following for my asp.net MVC application hosted in IIS 8.5:

Here is my current configuration in IIS:

My problem is that the application is shut down just fine at the specified hour (I make tests by adding another entry a few minutes in the future), but it doesn't restart after that, which I verified both in the log (Application_Start doesn't run) and with the long start time for the first request (around 15s while subsequent requests are around 0.07s).

Am I doing something wrong, or is IIS just unable to auto-start my application?

Upvotes: 3

Views: 3611

Answers (3)

Derek Massey
Derek Massey

Reputation: 1

It definitely is possible to have Applications Auto-Start. The part that no-one ever seems to mention is that you have to install "Application Initialization" to get it to work.

Server Manager --> Manage --> Add Roles or features --> Next --> Next --> Next Expand Web Server (IIS) --> Web Server --> Application Development And check "Application Initializion"

Upvotes: 0

youen
youen

Reputation: 2002

After trying with various settings, it turns out IIS won't ever load your application DLL until the first request, unless you give it a serviceAutoStartProvider class, as explained in https://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series

You must configure IIS like this:

  • App pool:
    • Start Mode = AlwaysRunning
    • Idle Time-out = 0
    • Disable overlapped recycle = True (not required if your application allows multiple concurrent instances)
    • Recycling / Specific Times = 03:00:00 (this is just an example, it will restart the application every day at the specified hour)

You also need to edit file C:\Windows\System32\inetsrv\config\applicationHost.config (only the relevant parts are shown here):

<configuration>
  <system.applicationHost>
    <sites>
      <site name="your site name" serverAutoStart="true">
        <application [...] serviceAutoStartEnabled="true" serviceAutoStartProvider="WarmUp">
        </application>
      </site>
    </sites>

    <serviceAutoStartProviders>
        <add name="WarmUp" type="WarmUp, YourApplicationDLL" />
    </serviceAutoStartProviders> 
  </system.applicationHost>
</configuration>

Then you must add a class in your application DLL:

public class WarmUp : System.Web.Hosting.IProcessHostPreloadClient
{
    public void Preload(string[] parameters)
    {
        // This is the entry point, but your website is not serving
        // pages yet here, so we can't send requests yet. Instead we
        // start a new thread and return immediately. You could add
        // initialization code that must run before serving any request.
        new Thread(DoWarmUp).Start();
    }

    private static void DoWarmUp()
    {
        // Here we just send a request to the home page to preload it.
        // This will run Application_Start, and compile .cshtml files
        // if you use Razor ; you may want to add more pages to preload
        using (var client = new WebClient())
        {
            client.DownloadData("http://localhost/MyHomePageIWantToPreload");
        }
    }
}

And that's it, now your web application is actually ready to serve the home page without any additional delay. If you have specific things in your application you'd like to preload, you may add more initialization code in DoWarmUp.

Upvotes: 2

Joshua Morgan
Joshua Morgan

Reputation: 678

I don't believe applications/sites IIS are able to restart themselves. I've looked into it several times over the years the most reliable way to achieve this is through scheduled tasks to call a page and keep the site alive.

This is a script.vbs file you can call from a scheduled task:

Option Explicit
Dim url, xmlhttp

Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
url = "http://example.com/page"
xmlhttp.open "GET", url, 0
xmlhttp.send ""

Set xmlhttp = Nothing

Upvotes: 2

Related Questions