4

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);
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
Ananth
  • 10,330
  • 24
  • 82
  • 109

3 Answers3

3

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.

matt
  • 333
  • 2
  • 10
  • 23
McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Thanks .Even I expected that the service would be called Synchronously. But some how the service is being called Asynchronously. So I had to add " waitHandle.WaitOne();" after the method is called so that I can continue debugging the code written in the called operation method. I don't understand why the service is behaving in this manner. The service was already written and I am just creating a client to test it. – Ananth Jun 14 '12 at 06:30
  • @Ananth what platform is your client app? If it's Silverlight, I seem to recall (it's been a while) that only asynchronous calls to WCF are allowed. – McGarnagle Jun 14 '12 at 06:33
  • Thanks.. Its a console Application – Ananth Jun 14 '12 at 07:08
  • Ive updated the question.. I see a line of code in the proxy class generated that I think is making it behave asynchronously. But I don't understand why that happens even though I have the "Generate asynchronous operations" unchecked.. :) – Ananth Jun 14 '12 at 07:09
  • @Ananth that's odd ... I wonder if it's some part of the service configuration that's making it asynchronous. Although, why do you need to wait for a response given that there's no return type ("void")? – McGarnagle Jun 14 '12 at 07:13
  • Thanks..Well I am trying to test the service operation for which I need to debug through the lines of code in the service.Since it is asynchronous, the application stops abruptly when the main thread in the console application ends. So Iam unable to complete my testing by covering all the lines of the code in the service operation. I have added "waitHandle.WaitOne()" as a work around that will let me do the work . I am curious to know why the service is behaving synchnously – Ananth Jun 14 '12 at 07:21
  • @Ananth ah, gotcha. Do you have access to the WCF service code by any chance? If so, could you post the XML configuration? – McGarnagle Jun 14 '12 at 07:24
  • Thanks.You need the config at the server or client ? – Ananth Jun 14 '12 at 07:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12528/discussion-between-dbaseman-and-ananth) – McGarnagle Jun 14 '12 at 07:28
  • As we discussed over the chat OperationContractAttribute(IsOneWay = true) was creating the issue.. Set it to false and it became synchronous..Please update your answer so that I can mark itas accepted answer – Ananth Jun 15 '12 at 04:14
  • @Anath sweet! Glad we figured it out. The answer is updated. – McGarnagle Jun 15 '12 at 04:20
1

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.

Eren Ersönmez
  • 38,383
  • 7
  • 71
  • 92
  • Thanks Erin.. I dont have the "Async" in the calling method. which means that the method is a synchronous method. But it is behaving in Asynchronous manner. Dont know for what reason. So I had to add " waitHandle.WaitOne();" after the method is called so that I can continue debugging the code written in the called operation method. – Ananth Jun 14 '12 at 06:33
  • @Ananth Could you also add the code where you're calling the method? If you call ABC (not BeginABC), it should be synchronous. – Eren Ersönmez Jun 14 '12 at 07:14
  • Thanks Erin..Iam calling ABC..Not BeginABC... Iam pasting the actual code here.. ABC was just hypothetical – Ananth Jun 14 '12 at 07:23
1

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.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 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.IAsyncResult BeginProcessCOBRecord(TestProject.ServiceReference1.ProcessCOBRecord request, System.AsyncCallback callback, object asyncState); void EndProcessCOBRecord(System.IAsyncResult result); – Ananth Jun 14 '12 at 06:54