tags:

views:

366

answers:

2

I created a WCF service where the class ServiceHost cannot be instantiated as a Singleton. I need to monitor every connection to this service, even before the first call to the methods associated with it. In practice, I would like to log when a client opens a channel to communicate with the service. Is it possible? In the web I find only two kinds of solutions:

  1. The ServiceHost object must be instantiated by the server and used as a singleton. Then I have to provide an initial method called by client to register itself to the service.
  2. Use performance monitor to show counters related to the service.

Neither (1) nor (2) satisfy my needs, because I want to create my own application and, as I said before, I cannot use the singleton mode. Have you ever encountered this kind of problem? How did you manage it?

Last, but not least, I need also to monitor calls to each method provided by the service. Is there a way to do that? Any help will be appreciated.

+1  A: 

There's a number of issues with this.

First of all, the preferred method of calling a WCF service is a per-call model, e.g. your client will call a service method, which causes an instance of the service class to be created on the server, the method in question is executed, and then the service instance is disposed of again. So you can't really monitor client connections per se - they only ever exist for fractions of a second while the call is executing.

Also, there's really not much infrastructure in place on the server side to monitor calls per second etc., other than the performance counters. The new server-addon product formerly known as "Dublin" (currently called "AppFabric") should bring quite a few improvements in that area (manageability) - see this MSDN article for more info.

But even today, you could envision to take the service class itself, and monitor instantiation and destruction of that class. The service class also has a link to the ServiceHost that instantiated it through the OperationContext.Current.Host property - so you could envision somehow signaling to the host that a new service class instance has been created. There's only goign to be a single host object, so that could work, but requires a well-thought out and well-tested multithreading-safe approach on the ServiceHost (you can create your own custom ServiceHost to achieve something like that).

That might be a step into a "monitoring my service" direction. As for performance monitoring - why don't the existing dozens of WCF performance counters help you or give you the info you need??

marc_s
Thank you for your answer but the solution you proposed doesn't meet my requirements. The constructor of the service class is called only when one of the service method is called and not when a client is connected to the service. I need this kind of info. Any idea?
Maurizio Reginelli
The client is "connected" to the service is identical to the service class being instantiated..... the client doesn't connect to a server or something - when a call comes in, the ServiceHost instantiates the service class and calls the method - that's all there is......
marc_s
Sorry, I think that the misunderstood was due to my low knowledge of WCF. I try to describe in another way what I need.When I write "when a client is connected to the service" I mean "when a client calls the open method", then before any call to methods provided by the service.I think that there should be a way to detect this operation.
Maurizio Reginelli
@Maurizio: no, there's nothing like that. You (1) create the client proxy, and (2) call a service method. THere's no ".Open()" or anything you need to do *before* calling the service method - there's no "connecting to the service" or anything. You just call a service method - and on the server, a service class instance is created to handle that call and then disposed of after the call is done.
marc_s
Thank you again. Now I can finally terminate my research... :-)
Maurizio Reginelli
A: 

Have you looked at WCF Tracing and WCF Message Logging ?

Subbu