Asynchronous WCF

I am trying to create a WCF service that supports asynchronous calls. I followed all the samples and tutorials that I could find, and they all have the usual scheme of one synchronous method and asynchronous start and end, for example:

[OperationContract(AsyncPattern = false)] string GetData(int value); [OperationContract(AsyncPattern = true)] IAsyncResult BeginGetData(int value, AsyncCallback callback, object asyncState); string EndGetData(IAsyncResult result); 

However, only synchronous GetData is called, regardless of what I do on the client side. Fiddler tells me that the message is always the same:

  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetData xmlns="http://tempuri.org/"><value>0</value></GetData></s:Body></s:Envelope> 

When I remove the synchronous GetData interface, the async method is now called correctly.

Is this normal behavior? Is there anything else I have to do to support synchronous and asynchronous versions of the method?

+4
source share
1 answer

This is a common misconception. You assume that you need to make the server asynchronous so that the client can make asynchronous calls. It is not true. Server and client are 100% independent. They are separated by a binary protocol.

The message you see in Fiddler is always the same because SOAP knows nothing about synchronization or asynchronous mode. At the SOAP level, your decision does not appear. For this reason, the client also cannot observe your solution on the server side.

This means that you can simply make the server synchronously still have the asynchronous client, or vice versa.

In any case, you should implement only one template on the server: either synchronize or asynchronously. Never. Get rid of one of your implementations. From a functional point of view, it does not matter which one remains.

I am extracting important information from the comments here:

It is difficult to explain when to use the server-side asynchronous system in this comment block. In short, do not use it on the default server. Use it if special circumstances make it attractive or necessary.

At the meta level, let me point out that async IO has become a fad that should not be followed lightly. The community is in a very unhappy state of misinformation about it right now.

+5
source

Source: https://habr.com/ru/post/1442599/


All Articles