How to detect WCF connectivity without try-catch?

1.2k views Asked by At

I use WCF netNamedPipeBinding and wsHttpBinding. I would like to know whether a one-way method can execute successfully without trying it.

EDIT As someone noted the conectivity can succeed a given moment and in the next fail. I don't care. I just want to know if WCF has an alternative to trying. Is there any way to check if the service is up and reachable?

3

There are 3 answers

3
VulgarBinary On BEST ANSWER

To my knoweldge the only way to determine if a one-way method executed successfully is whether or not it generates an exception.

If the method does not HAVE to be a one-way method you can return back a boolean value that states whether or not the method failed or succeeded.

Might I ask as to why you are trying to do this? Without catching an exception on the method there is no way to tell of error conditions if an exception does occur.

If I'm misunderstanding your use of "try ing" it and you mean without actually executing the method, you can write a proxy for the method that doesn't actually commit the action, only does the processing step.

If you are doing this for entirely testing purposes I would like to recommend taking advantage of the Visual Studio unit tests or wiring your project into NUnit.

Please provide more details of what you are trying to accomplish if one of the points above didn't answer your question.

EDIT: Modified to account for clarification by asker.

using(YourService svc = new YourService()){
    if(svc.State.Equals(!CommunicationState.Opened)){ /*Handle Error*/ } 
    svc.InnerChannel.Faulted += new EventHandler(YourFaultedEventHander); 
}

Will cover you to see if the connection is currently open AND will notify you if/when the service goes into a faulted state. This will preemptively let you know whether or not the call will succeed. However, there are still conditions that may cause that specific method to fail.. This will only notify you if you can successfully call the service method.

3
casperOne On

There's no way to know if a call to a one-way operation will be guaranteed to succeed; one-way operations (AFAIK) are supported on all bindings.

If you want to simply determine if the call is a one-way operation, then you will have to iterate through the operations on the service description (you aren't indicating if this is a client or server side call) and look at whether the operation is one way or not.

3
Brad Christie On

What happens if you test the call, it passes, then somehow the connectivity is dropped and your next code block fails? You're better off trying it, then handling the error if/when it appears.