Skip to main content

How to use IIS/WAS for calling a repeated task in C#


Ok, I found some useful information on the internet on the subject but some of it seemed... a bit wrong.
In my company we have several WCF websites and i just wanted to just plainly use IIS and not start to use some windows services for cron-jobs.
The task at hand was to periodically call a WCF method.

here is what i did:

Create a new project in Visual Studio. I chose a "WCF Service Application".

Go to the project properties and change and remember the Assembly name.
Create a new class "PreloadCode" like this:

 namespace XXX.WasAutostartTest  
 {  
         public class PreloadCode : IProcessHostPreloadClient  
         {  
                 private static Logger Log = LogManager.GetCurrentClassLogger();  
                 public void Preload(string[] parameters)  
                 {  
                         Log.Info("Preload called!");  
                         //do your custom code here... like me calling the WCF service!  
                 }  
         }  
 }  

now publish the website to
C:\inetpub\wwwroot\WasAutostartTest
start up iis manager, there add that website to IIS (make sure you have access rights) (dont forget the right .NET version, I know, I always do...) and name it "WasAutostartTest". now the tricky part begins...

go to the "configuration editor" in IIS-manager for the SERVER.
choose "system.applicationHost/serviceAutoStartProviders" as the section, press (...)
add an entry with:
 name=MyAutoStartProvider  
 type=XXX.WasAutostartTest.PreloadCode, XXX.WasAutostartTest  

now we need to edit one more thing, and it seems you can only do that in the favorite editor of your choice, which should be notepad++, but before you open it
make sure you have admin access, else you will have to store the changes to some other folder and then copy the save over the original.

go and open up
C:\Windows\System32\inetsrv\config\applicationHost.config
or the equivalent where your windows resides.

navigate to the node that starts with:
  <site name="WasAutostartTest".   
 make sure it has serverAutoStart="true" as its attribute. so it looks like this for me:  
 <site name="WasAutostartTest" id="5" serverAutoStart="true">  
but your ID will be different.
your application path should look something like this
  <application path="/" applicationPool="WasAutostartTest" serviceAutoStartEnabled="true" serviceAutoStartProvider="MyAutoStartProvider">  
           <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot\WasAutostartTest" />  
         </application>  
the important things here are
 applicationPool="WasAutostartTest" serviceAutoStartEnabled="true" serviceAutoStartProvider="MyAutoStartProvider"  
now save this.


to now make sure your code is called periodically, you just have to reset the recycle time of your "WasAutostartTest" application pool to the desired time!
for this, go to IIS manager again, in the tree select "application pools" select "WasAutostartTest", choose Advanced Options and under recycling set the "Regular Time Interval" to "5" for example!

This will make sure your code is called every 5 minutes!

I would now recommend to restart the website and restart IIS, just to make sure the changes are applied...


Troubleshooting

In case it seems that your code is not called then you should call one of the WCF methods to make sure that the code for the website is compiled... and then wait a while until the recycle time is reached and try again.

Ah yes, if you make mistakes and therefore it does not work as expected I recommend checking out the windows event viewers; he will show you if the application pool / IIS has troubles.

happy coding!


here are some of the websites that contained the information so i could come up with the solution


http://weblogs.asp.net/scottgu/auto-start-asp-net-applications-vs-2010-and-net-4-0-series
http://www.iis.net/configreference/system.applicationhost/serviceautostartproviders
http://tacticalnuclearstrike.com/2012/07/how-to-use-iprocesshostpreloadclient/
http://blogs.technet.com/b/meamcs/archive/2011/05/12/writing-an-iis-7-5-auto-start-provider.aspx

Comments