Which way? OneWay! (Web Services)
I’m currently studying for my next microsoft exam (70-529) that gets you certified for distributed development. I was reading in the training guide when someone asked me what the use was of the OneWay attribute.
So I explained to him that you can mark a web method with the OneWay attribute which indicates that this method can be called without the need to wait for a result. It’s the “Fire & Forget” principle. Sort of like the americans do in Iraq ![]()
So his first reaction was that it sounded the same like calling a procedure (sub in vb.net or void in C#) without it returning a result. Well yes and no. Yes because it has the same effect and no is in the difference that when you do a synchronous call to a web method that is a procedure the client will still wait for the acknowledgement of the webserver. So you make the call while the code on the server executes the client waits and when the code is finished the client is notified. While when you call a OneWay decorated method this doesn’t happen. The client just launches the call and continues working, it doesn’t wait for anything. So if your code takes a long time to execute in procedure the client will need to wait that long which it does not need to do with a OneWay method.
Okay this was clear, but he had another remark that was: “Why not mark all your procedures with OneWay as apparently it improves performance for the client ?”
Well that depends wether you want your client to be aware of certain exceptions that might occur on the server when the code there is executed. If you want this then you can’t use the OneWay method because of it’s nature the exception will never reach the client. You can test by just throwing any exception in the server code and decorating the erroneous method with the OneWay attribute. Then you call that method and you will see the nothing happens. The client just continues processing.
I already anticipated his next question that would be: “Is there an intermediate alternative than that I can use ?”. Well yes actually there is. Asynchronous method invocation.
There are several ways you can invoke web methods asynchronous:
- Create a new thread and execute the code synchronous on the separate thread.
- Create a new delegate with the same signature, instantiate it and then use the BeginInvoke and EndInvoke methods in collaboration with the IASyncResult interface.
- Just add a web reference to your project in vs.net and the asynchronous methods will be created for you.
If we take the last option you’ll see that when there is a synchronous method called “GetStockQuote” you can find the asynchronous counterpart as “GetStockQuouteAsync”. Calling this last method will execute the method asynchronously and you can continue processing other things. When you need to process a result you can attach to the Complete handler of the asynchronous method and in there process any results. This will only work of course when your method you are calling is a function. Also the important part is that when there is an exception it will be returned to the client.
These are certainly not all the possible solutions to work asynchronous but it demonstrates that you can simulate the effect of a OneWay method with the advantage of noticing any exceptions on the server.
