WCF Application Launch Event

What is the best way to get notified when you first start the WCF service?

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

+50
wcf
Apr 11 '09 at 0:48
source share
8 answers

Well, this can be a little tricky, since the preferred way to call WCF services is on a per-call basis, for example. you really have nothing that "started" and then just hung, really.

If you host your service in IIS or WAS, it is even β€œon demand download” of your service host - when a message arrives, the node is created and processes the request.

If you are using the host yourself, you have a Winforms console or application, so you can look in there to find out when they will start. If you have a Windows service to host your host, you will most likely override the OnStart and OnStop methods in the ServiceBase β†’ class.

The question is what exactly are you trying to accomplish? Just registering or something like that, or do you want something to be remembered to stick to?

Mark

+4
Apr 11 '09 at 7:29
source share

Since this is just a class, you can use a static constructor that will be called the first time you use Type.

public Service : IContract { public Service(){ // regular constructor } static Service(){ // Only called first time it used. } } 
+76
Apr 11 '09 at 0:54
source share

I used this link, there are several solutions: http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx

+22
01 Sep 2018-11-11T00:
source share

You can always manually add global.asax files to your WCF service application, because it is hosted in IIS and integrates with the ASP.NET pipeline:

 <%@ Application Codebehind="Global.asax.cs" Inherits="WcfApplication" Language="C#" %> public class WcfApplication : HttpApplication { protected void Application_Start() { } } 
+7
Jul 09 '14 at 7:41
source share

If you have a Self-Hosted WCF Service, you can add an event to the service opening, and inside this event you can assign a static variable, 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(); } } 

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

Good luck

+4
Jul 19. '11 at 19:50
source share
 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 
+1
Apr 24 '12 at 20:26
source share

The standard ServiceHost API for hosting in the Windows Communication Foundation (WCF) is an extension point in the WCF architecture. Users can derive their own host classes from ServiceHost, typically to override OnOpening to use ServiceDescription to force the addition of default endpoints or change behavior before opening the service.

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

0
Mar 18 '14 at 11:21
source share

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

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

Some assembly attributes are added to the WCF project.

 [assembly: WebActivatorEx.PreApplicationStartMethod ( typeof(MyActivator), "Start") ] public static class MyActivator { public static void Start() { // do stuff here } } 
0
Mar 02 '15 at 22:41
source share



All Articles