wcfvisual-studio-2010c#-4.0wcf-bindingwcf-behaviour

How to call a WCF service Synchronously


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);

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.