1

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?

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • Ok, so what if the method is up right now. So you call the method, but it has gone down in the meantime? Will you have your application simply crash, or will you use a try/catch block? – John Saunders Feb 24 '11 at 20:31
  • See the update in my initial comment. It covers notification of faulted state as well as determining the services current state. – VulgarBinary Feb 24 '11 at 20:47

3 Answers3

5

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.

VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
4

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.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • 3
    Someone finally understood the question – Jader Dias Feb 24 '11 at 20:13
  • 2
    I agree you will ALWAYS have to handle a service call with a catch, but there are ways (as I mentioned in my post) to determine the current state of the service as well as bubble up events to capture faulted states. Regardless, you should ALWAYS try{...}catch(..){...} your service calls no matter how much up front protection you code. – VulgarBinary Feb 24 '11 at 20:51
  • @JaderDias if the community is having trouble understanding the question, consider that the question may be a bad fit for the community. – Gusdor Nov 25 '15 at 11:42
2

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.

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • I know beforehand if the operation is One-Way. I just mentioned it as One-Way because I care just about the connection, not if the server threw exceptions. – Jader Dias Feb 24 '11 at 20:11
  • 1
    @Jader Dias: Without making the call, you can't tell; there is no built in "ping" functionality in WCF. You *could* call the `Open` method on the proxy before you make your call, but depending on the binding, that call can mean different things, the only thing the call guarantees is that the proxy can receive messages. – casperOne Feb 24 '11 at 21:59
  • That is not entirely true, yes the Open call can have different meanings depending on the binding (I guess an appropriate question here is to ask what binding Jader is using) but you can tell more about a service client without making a call. There is the fault event (which covers the case of a client channel is corrupted prior to a given call) and determining whether the channel is currently open. Which as you said only guarantees the proxy is ready to receive requests... but it still gives a status on the service beyond making the call blind. – VulgarBinary Feb 24 '11 at 22:17