I want to know whether the WCF service is available or not before making a service call.
What could be the best way?
How about using this:
bool isServiceUp = true;
try
{
string address = "http://localhost/MyService.svc?wsdl";
MetadataExchangeClient mexClient = new MetadataExchangeClient(new Uri(address), MetadataExchangeClientMode.HttpGet);
MetadataSet metadata = mexClient.GetMetadata();
// if service down I get the exception
}
catch (Exception ex)
{
isServiceUp = false;
}
My service is using net tcp binding.
can i use it for net tcp binding?
EDIT: Thanks JaredPar. Suppose my first call gets succeeded and while second call the server is down. So before making the service call i check the state of the proxy which is in the OPEN state and hence i make the service call which eventually gets time out. I haven’t set any open or close time out so by default it takes 1 minute and the call gets caught in the Fault event handler of the service in which i dispose the proxy. But by that time UI hangs , what shd i do?
Please guide.
This type of test is simply not possible. There is no way to reliably detect if a given WCF, or really any networking call, will succeed before you attempt it. There are simply too many variables which are outside your control.
Any of these can be terminated / changed at any point in time. Hence it’s impossible to predict whether or not a given call will succeed.
The best way to approach this problem is simply make the call add the code to handle the cases where it does fail.