When is the WCF service auto-started?

Damir Dobric Posts

Next talks:

 

    

Follow me on Twitter: #ddobric



 

 

Archives

Windows Server AppFabric offers the more or less well-known auto-start feature. Because many people seem to be confused with this I decided to post shorty few important facts.

How to enable Auto-Start?

1. To enable this feature you need to enable the it on application which host your service or on the service itself. I have described this in this article at TechNet..

2. You need to set the pool to AlwaysRunning. Open C:\Windows\System32\inetsrv\config\applicationHost.config and change the pool settings as shown below:
<applicationPools> <add name=”MyAppPool” startMode=”AlwaysRunning” /> </applicationPools>

How to figure out that it works?

1. Open following Operational-Event-Log, restart the pool and notice following messages:
imageimage

If these messages are not there or if some error/warning is to see, the auto-start didn’t work. If the service starts, but not in the expected pool apply following patch.

2. If you have a custom behavior attach debugger to it or trace-out some messages. After pool is restarted your behavior will be immediately executed.

3. If you have a singleton service the constructor will be invoked after pool has successfully started.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class Calculator : ICalculator
    {

        public Calculator()
        {
            Debug.WriteLine("AUTOSTARTED");
            Debugger.Break();
        }
  }
}

 

When is this feature helpful?

1. It enables the service warm-up. For this reason it is not necessary to send the first message to service to perform initialization.
Unfortunately this works only if your service is singleton (InstanceContextMode = Single) or if you initialization is implemented in one WCF extensibility point like behavior etc.

2. Some protocols, such as the WS-Discovery protocol, require applications to be always available. If you ever tried WCF-Discovery have have probably noticed that it does not work in IIS. This can be worked around by enabling auto-start of the service which has to be discovered.

3. This feature also enables hosting of services in IIS if they are configured to use service bus rely-bindings.

4. You can implement the singleton service whose constructor will be invoked when the pool starts or restarts. In this scenario you can implement the service functionality which is similar to classical  Windows Service.

5. daenet has implemented Workflow Activity which ensures that your service (implemented as activiy) is started after pool start (machine start). The good thing on this activity is that this implementation is in fact the implementation of singleton service service in cluster. Try to do it with any existing technology and you will see how difficult this can be. In other words WCF singleton implementation is not real singleton. .It is singleton at one machine or even application (site) only.

Is there anything what does not work, but is should?

Yes and No. Yes, because the most common issue is a service which is not a singleton, which use some static variables with long initialization time. Because the service is not singleton, the constructor is not called on auto-start. So the initialization cannot be done.
If you use extensibility points, this will help, but usually the service should never know about extensibility point and service should not know anything about extensibility point. To make it working extensibility point would perform initialization and set result to some variable like GLOBALCONTEXT.Result.
The service can read the result by GLOBALCONTEXT.Result. This will work, but it is very bad design and voted for ‘No

Hope this clarifies few auto-start gotchas.

Damir


Posted Oct 19 2011, 11:45 PM by Damir Dobric
Filed under: , ,

Comments

damir wrote re: When is the WCF service auto-started?
on 10-31-2011 23:05

You can also implement ServiceFactory: archive.msdn.microsoft.com/WCFAfAutoStart

Frank Szendzielarz wrote re: When is the WCF service auto-started?
on 01-05-2012 11:11

Thanks for that! Helpful.

developers.de is a .Net Community Blog powered by daenet GmbH.