Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need sample fire and forget async call to WCF service

On a scheduled interval I need to call a WCF service call another WCF Service asyncronously. Scheduling a call to a WCF service I have worked out.

What I think I need and I have read about here on stackoverflow that it is necessary to.., (in essence) prepare or change the code of your WCF services as to be able to handle an async call to them. If so what would a simple example of that look like?(Maybe a before and after example) Also is it still necessary in .Net 3.5?

Second I am using a proxy from the WCF Service doing the call to the next WCF Service and need a sample of an async call to a WCF service if it looks any different than what is typical with BeginEnvoke and EndEnvoke with typical async examples.

I would believe it if I am completely off on my question and would appreciate any correction to establish a better question as well.

like image 298
apolfj Avatar asked Apr 21 '09 21:04

apolfj


2 Answers

Set the IsOneWay property of the OperationContract attribute to true on the WCF method that you are calling to. This tells WCF that the call only matters for one direction and the client won't hang around for the method to finish executing.

Even when calling BeginInvoke your client code will still hang-out waiting for the server method to finish executing but it will do it on a threadpool thread.

[ServiceContract]
interface IWCFContract
{
   [OperationContract(IsOneWay = true)]
   void CallMe()
}

The other way to do what you want is to have the WCF service spin its work off onto a background thread and return immediately.

like image 152
joshperry Avatar answered Sep 17 '22 16:09

joshperry


Be sure to carefully test the way a OneWay WCF call performs. I've seen it stall when you reach X number of simultaneous calls, as if WCF actually does wait for the call to end.

A safer solution is to have the "target" code return control ASAP: Instead of letting it process the call fully, make it only put the data into a queue and return. Have another thread poll that queue and work on the data asynchronously.

And be sure to apply a thread safety mechanism to avoid clashes between the two threads working on that queue.

like image 32
urig Avatar answered Sep 20 '22 16:09

urig