Question

I have a WCF service and I am creating the client using the "Add service reference" from VS 2010.

The issue is that service is being invoked asynchronously though "Generate Asynchronous Operations" options unchecked.enter image description here

So how can I call the service Synchronously ? Where is this behavior defined (on the client or server) ? I am kind of new to WCF.Kindly enlighten

Client is a console application.

I have the "Generate asynchronous operations" unchecked. Even then the proxy contains the following lines which indicate that the method is called Asynchronously.Dont know why :)

 [System.ServiceModel.OperationContractAttribute(IsOneWay=true, Action="urn:COBService")]
    [System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
    [System.ServiceModel.ServiceKnownTypeAttribute(typeof(MemberType))]
    void ABC(TestProject.ServiceReference1.ProcessCOBRecord request);

    [System.ServiceModel.OperationContractAttribute(IsOneWay=true, AsyncPattern=true, Action="urn:COBService")]
    System.IAsyncResult BeginABC(TestProject.ServiceReference1.ProcessCOBRecord request, System.**AsyncCallback** callback, object asyncState);

    void EndABC(System.IAsyncResult result);
Was it helpful?

Solution

Update

It turns out the WCF service configuration was causing this asynchronous behavior, specifically the IsOneWay property of the OperationContract attribute. This is not technically asynchronous, but it "usually gives the appearance of asynchronous call".


You don't have to do anything special, just invoke the normal method on the client proxy -- that's the synchronous method. So if you have a WCF method named DoSomething, then you'd just call:

var client = new MyService.MyServiceClient();
client.DoSomething();

It's client.DoSomethingAsync that is the asynchronous method.

This distinction relates to the client behavior, whether or not your application blocks the thread while waiting for the WCF service to respond.

OTHER TIPS

If the Generate asynchronous operations option is unchecked then the service will be called synchronously

From MSDN

Generate asynchronous operations
Determines whether WCF service methods will be called synchronously (the default) or asynchronously.

After you're done adding the service reference you should get synchronous methods for each exposed service operation.

Sychronous methods are named the same as the service operation, e.g. GetCustomers. Async methods on the other hand are generated in two ways: GetCustomersAsync, BeginGetCustomers/EndGetCustomers.

If you want to get customers synchronously, you need to call GetCustomers. In that case, GetCustomers will block until the service operation is completed and then the code moves on the next line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top