WCF application start event

What is the best way to get notified when a WCF service is first started?

Is there something similar to the Application_Start method in the Global.asax for an ASP.NET application?


Since it's just a class, you can use a static constructor which will be called the first time the Type is used.

public Service : IContract
{
    public Service(){ // regular constructor }
    static Service(){ // Only called first time it's used. }
}

Well, that might be a bit tricky since the preferred way of calling WCF services is on a "per-call" basis, e.g. you don't really have anything that's "started" and then just hangs around, really.

If you're hosting your service in IIS or WAS, it's even "on-demand loading" of your service host - when a message arrives, the host is instantiated and handles the request.

If you self-host, you either have a console or Winforms app - so you could hook into there to know when they start. If you have a Windows service to host your service host, you most likely override the OnStart and OnStop methods on the ServiceBase class --> hook into there.

The question is more: what exactly are you trying to accomplish? Just logging or something like that, or do you want to have something built up in memory to stick around??

Marc


You can always manually add global.asax files to your WCF Service Application as it hosted on IIS and integrates with ASP.NET pipeline:

<%@ Application Codebehind="Global.asax.cs" Inherits="WcfApplication" Language="C#" %>

public class WcfApplication : HttpApplication
{
    protected void Application_Start()
    {
    }
}