Search Wiki:

WCF / Windows Server AppFabric Auto-Start Sample


Windows Server AppFabric includes a feature that allows you to auto-start a service. This sample application demonstrates how auto-start improves performance and also how you can initialize services by creating a custom service host factory.

Video

endpoint.tv - WCF and AppFabric AutoStart

What does auto-start do?

It depends on your particular service but there is a fair bit of work that has to be done when starting a service. The work includes setting up ASP.NET, spinning up an appdomain, compiling (if required) and some other misc things. If you want the details use Reflector to look at Microsoft.ApplicationServer.Hosting.AutoStart.ApplicationServerAutoStartProvider and System.ServiceModel.Activation.ServiceHostingEnvironment.EnsureServiceAvilable as these classes do the work. One thing it does not do is create an instance of your service class or call any methods on it.

How much faster is the first call to my service if I use auto-start?

A lot faster. Try an order of magnitude faster. In testing a service published to two IIS Web applications, one with Autostart and one without, the results show the call to the auto-started service was significantly faster.

How can I write code that is called when the service is auto-started?

You can create a custom service host factory that does the initialization.

public class TestServiceHostFactory : ServiceHostFactoryBase 
{     
   public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)     
   {         
      ProcessEvents.AddEvent("TestServiceHostFactory called");         
      TestCache.Load();         
      return new ServiceHost(typeof (TestAutoStart), baseAddresses);     
    } 
}  

In your markup for your .SVC file let WCF know you are using a custom service host factory

<%@ ServiceHost Language="C#"  Debug="true"  Service="AutoStartWebTest.TestAutoStart"  CodeBehind="TestAutoStart.svc.cs"  Factory="AutoStartWebTest.TestServiceHostFactory" %>  

Note: You can also use configuration to define a custom service host factory

Of course this requires that you do this for every service that needs special initialization. You cannot use Application_Startup from global.asax to do this because it won't be called.

The IIS Warmup Module for IIS 7.5 may be another helpful option that will work without requiring you to implement a custom service host factory.

Setup


To see this demo in action, publish the application to the local IIS server in two web applications
  • AutoStartWebTest
  • NoAutoStartWebTest

Configure the AutoStartWebTest service to autostart all services
  1. Right click on AutoStartWebTest and select Manage WCF and WF Services
  2. Select Configure
  3. On the Auto-Start tab select Enabled (all services will auto-start)

Run the AutoStartClient to see the services in action
Remember to recycle the web application to see auto-start take effect
Last edited Dec 10 2010 at 3:28 PM  by RonJacobs, version 3
Updating...
Page view tracker