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?
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. }
}
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()
{
}
}
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
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!
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
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
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
}
}