Waiting for completion of one-way WCF call in a C# console application

992 views Asked by At

I have a WCF webservice (not under my control) that implements functionality I need to access via IsOneWay=true + a callback interface, one of the methods of which notifies of processing completion. It has been written this way as it was initially designed for access from a GUI.

However I need to access this same method from a console application for use in a batch. Currently my crude method of achieving this is to set a flag to false and after calling the WCF method I implement a while loop with a brief Thread.Sleep() call in it. This obviously works but seems like a really poor way of achieving the end result.

I'd like to know what the proper way of doing this is. Note: the service is out of my control and the reference has just been added through the IDE although I can easily knockup a code implementation etc.

1

There are 1 answers

1
Muhammad Hasan Khan On

Use a ManualResetEvent for waiting for completion. Call the one-way async method on the service and then wait on the manualresetevent object. In your callback call the Set() method which will let your main thread continue execution after WaitOne() call like so:

var waitHandle = new ManualResetEvent();

yourwcfObject.CallTheOneWayMethod();
waitHandle.WaitOne();


void CallbackMethodRaisedByWCFService()
{
    waitHandle.Set();
}