How to monitor client connection to a WCF service?
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:
- The开发者_如何学Go 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.
- 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.
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??
Have you looked at WCF Tracing and WCF Message Logging ?
精彩评论