2

I have a WCF service that is hosted by a windows service. I can't figure out how to inform the windows service when a client connects to the WCF service. Basically all I have in the windows service to start the WCF service is this:

private ServiceHost sHost;
WCF.WCFService wcfService = new WCF.WCFService();
sHost = new ServiceHost(wcfService);
sHost.Open();

I am able to call methods in the WCF service with the windows service using the wcfService object. Is there some way to have some kind of event that would fire when a client connects to WCF service?

Brian
  • 1,397
  • 9
  • 33
  • 51

2 Answers2

1

The service runs as an object which is instantiated according to the ServiceBehaviourAttribute property InstanceContextMode

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{ 
    // ...

The values for InstanceContextMode are

  • Single - a single instance of the service runs for all sessions and calls
  • PerSession - an instance of the service runs for each session (i.e. each client)
  • PerCall - an instance of the service is instantiated for each call, even from a single client

The default value is PerSession, and this makes sense for most scenarios. Assuming you're using PerSession, you can put whatever 'connect logic' you like inside the constructor for the service.

// you don't need to specify PerSession as it is default, but I have for clarity
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyService : IMyService
{ 
    public MyService()
    {
        // constructor will be called for each new client session
        // eg fire an Event, log a new client has connected, etc
    }
    // ...
}

You need to be careful running code in the constructor, because the service will not be available until the constructor has completed. If you want to do anything that may take time, fire an event or send a thread to perform that work.

Kirk Broadhurst
  • 27,836
  • 16
  • 104
  • 169
  • i'm not sure if I understand exactly what you ware saying, but it seems to me like having logic in the constructor would not really let the host know about a new connection. It would just be executing logic within the WCF service when a new server is started. – Brian Sep 22 '11 at 06:07
  • Which part don't you understand? Every new connection starts a new instance of the service, therefore every new connection calls the service constructor. – Kirk Broadhurst Sep 22 '11 at 06:15
  • yes, I understand that, but how then does the HOST (in my case the windows service) know about the connection? The constructor is within the WCF service. – Brian Sep 22 '11 at 06:24
  • I think I understand - you're asking how the event can/will propograte from the service up to the windows service? If that's your question then you can either use an [event](http://stackoverflow.com/questions/2916364/what-is-event-in-c) or a [delegate](http://stackoverflow.com/questions/2077712/what-is-delegate-and-how-it-allows-to-create-objects) – Kirk Broadhurst Sep 22 '11 at 06:48
1

I found the best answer here: Subscribe to events within a WCF service

As suspected you can create an event handler in the WCF service that can be picked up by the host.

Community
  • 1
  • 1
Brian
  • 1,397
  • 9
  • 33
  • 51