One-way async / wait calls in WCF

I have an outdated WCF service with several one-way work contracts implemented in the most classic way:

// The interface [OperationContract(IsOneWay = true, AsyncPattern = true)] IAsyncResult BeginFinalizePublishing(PageContent pageContent, AsyncCallback callback, object asyncState); void EndFinalizePublishing(IAsyncResult result); // The service public IAsyncResult BeginFinalizePublishing(PageContent pageContent, AsyncCallback callback, object asyncState) {...} 

So everything works fine, now I need to convert this to C # 5 the latest async / await template. Pay attention to the one-sided nature of operations. My questions:

  • I think I need to remove AsyncPattern = true from the OperationContract attribute.
  • Replace the return type of IAsyncResult with void in the interface and async void in the class.
  • Remove the start / end and add the Async postsynchronizer for convenience.

Now, what can I call a one-way transaction with a client? No use of async / await on the client side for one-way calls?

This is correct and will result in true asynchronous client-side processing.

  // The proxy public void FinalizePublishingAsync(PageContent pageContent) { Channel.FinalizePublishingAsync(pageContent); } // The consumer of the client // No way to use await for void methods. IComPublisherFinalizerClient cl = new IComPublisherFinalizerClient(); cl.FinalizePublishingAsync(content); cl.Close(); 
+6
source share
1 answer

First, the client is completely separate from the server. Any client or server can be synchronous, use asynchronous IAsyncResult code (APM) or use async code (TAP).

Secondly, server-side async methods should return Task , even for unidirectional communication. The async equivalent of the synchronous method returning void is the async Task method, not the async void method. async void especially dangerous in WCF services because it can violate ConcurrencyMode .

So, your server side steps will look like this:

  • Remove OperationContract.AsyncPattern .
  • Combine a pair of Begin* / End* methods into one *Async method, which returns a Task .

Your client side should see Task regular methods on the interface for its proxy, which should await . This does not mean that you expect an answer; this await allows you to wait for the actual message to be sent (if there are delays) and detect channel communication errors (if you are using a reliable channel).

+6
source

All Articles