Reputation: 81
I have written a C# WebService. The problem is that after I publish it to IIS it won't automatically start unless any of its methods is called. This is very frustrating because this WebService has to continuously do some background work immediately after it starts (its constructor executes). If IIS is restarted, the WebService will just sit idly until one of its methods is called. Is there a way to overcome this and force the WebService to execute its constructor immediately after it is published or IIS restarted?
Upvotes: 8
Views: 9474
Reputation: 11
IIS 7.5 supports this requirement by an extension, Application Initialization Module for IIS 7.5, http://www.iis.net/downloads/microsoft/application-initialization.
IIS 8.0 has built-in support: http://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-application-initialization.
Upvotes: 1
Reputation: 576
Actually, you can run a Thread or a job automatically in "Global.asax.cs". E.g.
public System.Threading.Thread schedulerThread = null;
protected void Application_Start(Object sender, EventArgs e)
{
schedulerThread = new System.Threading.Thread(YourLoopBackgroudMethodHere);
schedulerThread.Start();
}
And Don't forget to close the thread when your site application end.
protected void Application_End(Object sender, EventArgs e)
{
if (null != schedulerThread)
{
schedulerThread.Abort();
}
}
Upvotes: 1
Reputation: 62157
And I do not agree. There are good reasons to make sure that a web service is started with the computer - to allow it to start filling it's caches and / or to simlpy avoid the slow first execution.
The IIS team is with me on that. This is why they developped soemthing for that:
http://www.iis.net/expand/ApplicationWarmUp
This module can make sure a website is "warm" (as in: started) when IIS starts.
Upvotes: -1
Reputation: 10884
I agree with the others, you should use a windows service. If you're using WCF, you can easily host your web service inside a windows service and get the best of both worlds. See this msdn article for a tutorial.
Upvotes: 1
Reputation: 181044
Apart from the (correct) remark that you should use a Windows Service for stuff that needs to run in the background: Can you use the Application_Start Event in the global.asax?
Upvotes: 1
Reputation: 6122
You need to create windows service. Here are the steps to create windows service for scheduled task.
Upvotes: 1
Reputation: 16623
I don't think a web service is meant to do some background work continuously - it's there to serve requests for its methods.
Upvotes: 3
Reputation: 117320
If you need it to run all the time, your design is flawed. Create a windows service instead.
Upvotes: 4
Reputation: 15139
If It has to continuously do some background work immediately after it starts why not implement this in a Windows service? I think you can write a WCF service which will be hosted in the Windows service. That way clients can still call your service, the service can do its background work and wont be dependent on IIS as the host window service will run on its own process.
Upvotes: 10