59

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?

1

7 Answers 7

87

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. }
}
4
  • 1
    I was hoping for something more host-specific, i.e. something in the application that's hosting the service.
    – user89166
    Apr 11, 2009 at 1:01
  • 7
    paul answer worked for me. Static constructor is the savior!!
    – CodeNinja
    Feb 11, 2011 at 10:09
  • 7
    Perfect. And today I learned about static constructors for the first time.
    – eouw0o83hf
    Mar 20, 2012 at 20:07
  • Yet another option would be using a custom service host factory and put a static constructor there. This will work both for multiple services and IoC containers which are using custom factories. Say if you are using Autofac.Integration.Wcf that will do the trick: public class MyHostFactory : AutofacServiceHostFactory { static MyHostFactory() { /* init code goes here */ } } Jun 21, 2017 at 15:11
9

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

8
  • 88
    (-1) I really don't understand how this "answer" helps anyone. You basically asked him back and didn't answered his question. Oct 16, 2009 at 5:02
  • 11
    I tried to explain that there's nothing exactly like Application_Start in WCF, but tried to provide ideas where he could hook into to detect what he needs to detect.... sorry you feel that way - I tried my best, but feel free to provide a better answer!
    – marc_s
    Oct 16, 2009 at 7:40
  • 4
    Paul Alexander supplied a better answer that actually helped me a lot. Oct 16, 2009 at 10:16
  • 3
    This answers the question - there is no start event for the application, they're on demand. Presumbly there must be a way of tying into the AppDomain.AssemblyLoaded but at that point it's too late
    – Chris S
    Aug 25, 2010 at 10:43
  • I guess Application_Start might be appropriate if you wanted to do something that was specific to HTTP binding such as initialising SimpleMembership so that the web service can then use roles/membership functions internally.
    – Monstieur
    Feb 13, 2013 at 12:01
9

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()
    {
    }
}
3
  • Can you expand your answer, further? What exactly are you inheriting, how does this work? I think your answer might help me, personally
    – Thomas
    Dec 8, 2014 at 19:50
  • It's pretty much as if you created a new web api project from the default template. Dec 9, 2014 at 12:36
  • Calling wcf service in this case should not call Application_Start. At least by TCP protocol. It's a problem. Jul 9, 2019 at 13:38
5

If you have a Self-Hosted WCF Service, you can add an Event to the Opening of the service, and inside this Event you can assign a static variable, just like this post:

//Static Variables in a WCF Service
public class Post2331848
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]    
        string GetString();
    }    

    public class Service : ITest
    {
        public static string TheString; 
        public string GetString()
        {
            return TheString;
        }
    }

    static void host_Opening(object sender, EventArgs e)
    {
        Service.TheString = "This is the original string";
    } 

    public static void Test() 
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 

        //This is the magic line!
        host.Opening += new EventHandler(host_Opening);

        host.Open();

        Console.WriteLine("Host opened"); 
        Console.ReadLine();
        host.Close();
    }
}

(Originaly from http://www.eggheadcafe.com/community/aspnet/18/10162637/help-in-maintain-global-variable-in-wcf.aspx)

Good Luck!

2
Imports System.ServiceModel
Imports System.ServiceModel.Description

Public Class MyServiceHost
   Inherits Attribute
    Implements IServiceBehavior

    Public Sub AddBindingParameters(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase, endpoints As System.Collections.ObjectModel.Collection(Of System.ServiceModel.Description.ServiceEndpoint), bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) Implements System.ServiceModel.Description.IServiceBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyDispatchBehavior(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior
        AddHandler serviceHostBase.Opened, AddressOf serviceHostBase_Opened
        AddHandler serviceHostBase.Closed, AddressOf serviceHostBase_Closed
    End Sub

    Public Sub Validate(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.Validate

    End Sub




#Region "Event Handlers"


    Private Sub serviceHostBase_Opened(ByVal sender As Object, ByVal e As EventArgs)



    End Sub

    Private Sub serviceHostBase_Closed(ByVal sender As Object, ByVal e As EventArgs)



    End Sub


#End Region
0

The standard ServiceHost API for hosting services in Windows Communication Foundation (WCF) is an extensibility point in the WCF architecture. Users can derive their own host classes from ServiceHost, usually to override OnOpening to use ServiceDescription to add default endpoints imperatively or modify behaviors, prior to opening the service.

http://msdn.microsoft.com/en-us/library/aa702697%28v=vs.110%29.aspx

0

There is a nuget package called WebActivator that I found useful for IIS hosting.

https://www.nuget.org/packages/WebActivatorEx/

You add some assembly attributes to your WCF project.

[assembly: WebActivatorEx.PreApplicationStartMethod
(
    typeof(MyActivator),
    "Start")
]

public static class MyActivator
{
    public static void Start()
    {
        // do stuff here
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.