Friday, 24 February 2012

Command Script Using Appcmd.exe to configure AutoStart WCF in IIS

In this blog entry I will talk about how to configure auto starting WCF / HTTP exposed services. I will provide a script below that can be executed to enable auto start for the website.

In IIS, you can configure an application pool and all or some of its applications to automatically start when the IIS service starts. This is available in (IIS) 7.5 which is included in Window 7 or Windows Server 2008 R2.

When you enable the auto-start feature for a service, the service is up and running as soon as the application that it belongs to is started and before the service receives the first WCF message from the client. Therefore, the service processes the first message quickly because it is already initialized. For example, suppose a service needs to connect to a database to read hundreds of rows of data to populate a .NET Framework caching data structure when it is first created. The initialization processing takes a long time before the service is ready to begin the actual operation. If you use auto-
start in this case, the service is initialized before it receives the first call.

Lets look at the script below which will create and autostart an application pool.

Firstly we start with creating a new application pool call MyAutoStartAppPool
%windir%\system32\inetsrv\APPCMD.exe add apppool /name: MyAutoStartAppPool
We then set the properties autoStart:true and startMode: AlwaysRunning
Also I set the .NET framework to v4.0 and Enable 32 bit processing. These can be skipped if not required.
%windir%\system32\inetsrv\APPCMD.exe set apppool MyAutoStartAppPool
 /managedRuntimeVersion:v4.0 /autoStart:true /startMode:AlwaysRunning
%windir%\system32\inetsrv\APPCMD.exe set apppool MyAutoStartAppPool
We then set the identity of the app pool
%windir%\system32\inetsrv\appcmd.exe set config /section:applicationPools
Finally we then set the website in this case “Default WebSite/” to the application pool created. Then we set the website serviceAutoStartEnabled: true serviceAutoStartMode: all and serviceAutoStartProvider: Service
%windir%\system32\inetsrv\appcmd.exe set app /"Default WebSite/"
 /applicationPool: MyAutoStartAppPool 
%windir%\system32\inetsrv\appcmd.exe set app /"Default WebSite/"
 /serviceAutoStartEnabled:True /serviceAutoStartMode:All /serviceAutoStartProvider:Service

Now any hosted WCF service or website is autostarted. I sometimes put processing code and populate memory caches in global.asax.cs in the Application_Start method.

This autostart feature makes the code startup quicker and stay started.

1 comment:

  1. You are excellent. Your post about about how to configure auto starting WCF / HTTP exposed services is in very details.In a word,i'm just new to this filed.I met a lot of problems in my work.Your article is written very well, there is a lot of very useful knowledge to help me solve problems.I enjoy reading your article and hope to see more.

    Tags: .NET Framework AutoStart WCF IIS