WCF: Availability of the WCF services

The requirements look easy:
We need to monitor the health of the WCF services, availability of the WCF services. Are they up or down?
But we don't want to call operations of the service  because it sometime could change the service internal status and these changes are out of our control.
Some services have special Heartbeat() operation to make the health monitoring easily, but the most have not.
We don't have to check the work status of the web-method. That means we don't care if some underlying services, like SQL databases, don't work and the services available but response with errors. We just want to know are web-services up or down, could they response at all or not. It should be something really easy.
 
Let's start:
I thought this is quite routine task to the service consumers and there should be several routine decisions.
As I know the OpsMngr2007 use something  simple to measure the web-service health.
 
I've asked gurus on the MSDN forums about this.
 
Dan Rigsby proposed:
"A one way Ping() method on the server works well.  If there is an exception, then the channel is broken.  You can have a timer run Ping against the server at some interval.
If this is a duplex callback system you need to be careful.  The server might also need to check if the client is still alive.  You cant have both the servicecontract and callbackcontract have the same named methods, so you will have to rename one of them."
 
Thanks, Dan!
 
I've tried to
      >ping http://text.company.com/adfadfad/asdfadf/adfadfadf.asmx
and
     >ping http://text.company.com/adfadfad/asdfadf/adfadfadf.asmx?wsdl
 
The ping works only for the host name, like
      >ping text.company.com
not for web-service URL
 
It is not what I want.
 
It checks the health of the host, not the web-service.
It does not cover cases with several services on the one host.
It does not cover cases when the host is OK but the service is not response.
 

Islam Eldemery proposed:
"try to subscribe to the events of the communication object (in the client side), so if a service stopped or disconnected you may handle the fault event.. (remember that it can only fault once).

      proxy.InnerChannel.Faulted +=
new EventHandler(InnerChannel_Faulted);

or
      proxy.InnerDuplexChannel.Faulted += new EventHandler(InnerDuplexChannel_Faulted);

see my example of handling a service in a chat application
http://www.codeproject.com/KB/IP/WCFWPFChatRoot.aspx
if this is a duplex communication, to handle the service on the service side, try to check every time on each callback object you have, to see if someone has been aborted or not before you using it (may be a client disconnect for a network failure.. etc.)"

Islam, thanks for response!

 Unfortunately with this technique we can intercept only errors after we unsuccessfully call the service operation, right?
It is not our case. We want to check the availability not "post mortem" but proactively. For instance, click a button and get health of all services. And we cannot call the service operations, it's one of requirements.
 
Martin16 proposed:
"...
Additional ideas (for IIS/HTTP WCF Services) are 
1) a custom availability operation in your service which checks all resources and gives back a result your web application monitor can parse.
2) enable WCF performance counter and create a monitor on top of that.
3) buy Avicode MP (should provide native WCF Service support)"
 
Thanks for help, Martin16!
 
Yeah, 1) is the Heartbeat() operation. It doesn't work in my case where the services are out of mu control.
For 2) First, we want actively to request the health status not to listen how the service responses to the requests of the other clients.
Second, the WCF performance counters... OK I use them with OpsMng2007 I don't like them very-very much. (It's another story).
3) The third-part utils should use the same APIs we use, not something secret.  I also use OpsMngr2007. But I want to use simple and absolutely open (to me) source code.
 
...
So far so good.
Is it possible to use Mex endpoint or something like HttpRequest to <URL>?wsdl to make sure the service is up?
 
I've got one method from the book "Programming WCF Services" by Juval Lowy (page 71). 
Code Snippet:

   MetadataExchangeClient mexClient =

bool isServiceUp = true;
try
{
  
string address = "http://localhost/MyService.svc?wsdl";new MetadataExchangeClient(new Uri(address), MetadataExchangeClientMode.HttpGet);
   MetadataSet metadata = mexClient.GetMetadata();
// if service down I get the exception
}
   catch (Exception ex)
{
    isServiceUp =
false;
}
 
I catch the Exception like {"Metadata contains a reference that cannot be resolved: "http://localhost/MyService.svc?wsdl"} System.Exception {System.InvalidOperationException}
 
Easy and don't heavy load the service.
P.S.    Please, make sure you've read good comment by Islam Eldemery below.
================================================================
Additions:
[2009-07-14]
I moved here a text from the discussions below:
 
Finally, I was creating the custom solution with Heartbeat().
Of course only some Web-services has this method, then I used the regular methods in specific fashion.
1. I must use the method that doesn't change the inner state of the service (logging is OK).
2. I didn't want to overload the service.

I used the insider information about these services.
The idea is to get the ERROR response. I just want response. And the error is a good solution. It doesn't change the service status, it does not overload the service.

The bad thing with the error response is it strictly depends of the implementation. Than means it work only if you know enough about insides of the service.
Other bad thing is you have to implement the pseudo-Heartbeat() in specific way for different services. It consumes the development time.
Conclusion (up-to-date):
Creating a service with high availability, create a Heartbeat() method to provide clients with instrumentation to check the availability of this service. BTW This method could return a valuable information about current/max performance, payload, etc.
========================================================================================
Print | posted on Wednesday, May 7, 2008 11:48 AM

Feedback

# re: WCF: Availability of the WCF services

left by Islam Eldemery at 5/7/2008 1:13 PM Gravatar
Well, this is a good, easy way to check the availability of a service a doesn't make a load on it.

I have 2 arguments,

first, take care you may get this exception "Metadata contains a reference that cannot be resolved" although the service is up and running, this may occur if the metadata size exceeds the maxReceivedMessageSize.

second, what if the service doesn't enable metadata http Get? (As you said you don't have control over the serice).

# re: WCF: Availability of the WCF services

left by Colin Jack at 10/20/2008 9:18 AM Gravatar
I'm interested to know what approach you went with, seems like there should be a good solution to this but polling a HeartBeat method is the best I've seen so far (assuming of course you control the WCF service and can change its API).

# re: WCF: Availability of the WCF services

left by John Smith at 1/8/2009 3:37 PM Gravatar
Instead of writing code to do all this, might be better off looking at tools like AmberPoint (www.amberpoint.com) that provide WCF monitoring and management out-of-box. I wish there was a free version though.

# re: WCF: Availability of the WCF services

left by Will Rogers at 2/10/2009 6:38 AM Gravatar
Hello Leonid. It seems you misunderstood Dan Rigsby's suggestion. He was suggesting the same Heartbeat()-type function as others did. In his case he was suggesting calling the service operation Ping(). He did not actually intend for you to use the Windows Ping command.

From what I've read there is no way to tell if a communication channel will work until you actually use it and notice that it worked or it didn't. I refer to this as the Schroedinger's Cat paradox of communication channels. This is also why heartbeat-type function calls can only tell you that a channel *WAS* open, not that it *IS*. Ultimately you're in a race-condition.

On the service-side, I think the WCF Perf counters (not sure which ones) as mentioned by Martin16 are your only choice if you cannot modify the services you're monitoring.

# re: WCF: Availability of the WCF services

left by Leonid Ganeline at 2/10/2009 11:23 AM Gravatar
John,
I have to take a look at AmberPoint, sure.

Colin, Will,
Finally, I was creating the custom solution with Heartbeat().
Of course only some Web-services has this method, then I used the regular methods in specific fashion.
1. I must use the method that doesn't change the inner state of the service (logging is OK).
2. I didn't want to overload the service.

I used the insider information about these services.
The idea is to get the ERROR response. I just want response. And the error is a good solution. It doesn't change the service status, it does not overload the service.

The bad thing with the error response is it strictly depends of the implementation. Than means it work only if you know enough about insides of the service.
Other bad thig is you have to implement the pseudo-Heartbeat() in specific way for different services. It consumes the development time.

# re: WCF: Availability of the WCF services

left by Mrunal Buch at 2/12/2009 5:20 AM Gravatar
Hi, Leonid (and others)

Can you guys provide some sample code to implement the Heartbeats. I have a service registry which gives away ABC when requested. I now want to implement some sort of monitoring to detect whether the service is up or not.

Any pointers ? Thanks in advance.

# re: WCF: Availability of the WCF services

left by mitri at 2/20/2010 10:15 AM Gravatar
Very Good comment by Islam

# re: WCF: Availability of the WCF services

left by Ajander Singh at 4/14/2010 2:14 AM Gravatar
very good comments

# re: WCF: Availability of the WCF services

left by Reddy at 4/15/2010 3:01 AM Gravatar
Hi, Leonid Ganeline,

I have the same requirement to check few wcf sevices are up or down in my project. Could you please let me know the final solution and if possible please post the piece of code.

Thanks in advance,
Reddy

# re: WCF: Availability of the WCF services

left by Stef Sewell at 10/18/2011 2:02 PM Gravatar
We had the same requirement for a heartbeat method and implemented one as a WCF endpoint behavior. This can be added to a service via config, similar to the MEX endpoint.

See http://web.me.com/stefsewell/TechEd2010/ASI02-INT/Entries/2011/10/16_Checking_WCF_Service_Availability_using_an_Endpoint_Behavior.html

Cheers,
Stef

# re: WCF: Availability of the WCF services

left by shobha at 3/2/2012 4:25 AM Gravatar

MetadataExchangeClient mexClient =

bool isServiceUp = true;
try
{
string address = "http://localhost/MyService.svc?wsdl";new MetadataExchangeClient(new Uri(address), MetadataExchangeClientMode.HttpGet);
MetadataSet metadata = mexClient.GetMetadata();
// if service down I get the exception
}
catch (Exception ex)
{
isServiceUp = false;
}

this code is incomplete can you give complete code

# re: WCF: Availability of the WCF services

left by Imran at 1/11/2013 2:59 AM Gravatar
My Service client communicates with service using net.pipe protocol as the service is hosted in the same machine. I am unable to use MetadataExchangeClient as HTTPGet is not supported here. Can some one suggest solution in this case ?
Post A Comment
Title:
Name:
Email:
Comment:
Verification: