Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add async support to a .NET 4.5 WCF service so it doesn't disrupt existing clients?

I have an existing WCF service with a SOAP endpoint, using .NET 4.5. Most of the existing client code is using the ChannelFactory<T> proxy approach.

I'd like to change the service to support the async / await model for various server-side I/O and database operations.

The problem I'm having is that adding the async keyword to the WCF method calls requires changing their interface signatures to Task<T>. That, in turn, seems to be requiring changes to the client code.

While keeping the service code async "all the way down," is there a straightforward way to keep the exposed API unchanged?

like image 209
RickNZ Avatar asked Jan 11 '15 00:01

RickNZ


People also ask

How do you call async method in WCF?

To call a WCF service method asynchronously you need to create an AsyncCallback delegate and pass this delegate to the asynchronous proxy method. Note that the AsyncCallback delegate is executed on a worker thread and so this doesn't block the main thread of the application.

Is WCF asynchronous?

In Windows Communication Foundation (WCF) applications, a service operation can be implemented asynchronously or synchronously without dictating to the client how to call it. For example, asynchronous service operations can be called synchronously, and synchronous service operations can be called asynchronously.

Should I use async await everywhere C#?

Async/Await has been around since C# version 5.0 (2012) and has quickly become one of the pillars of modern . NET programming — any C# developer worth his/her salt should be using it to improve application performance, overall responsiveness, and code legibility.

Does async await improve performance?

The main benefits of asynchronous programming using async / await include the following: Increase the performance and responsiveness of your application, particularly when you have long-running operations that do not require to block the execution.


1 Answers

As long as you rename your server side method to include the word XxxxxAsync it will not change the clientside signature.

WCF automaticly makes two endpoints for every method, a synchronous version and a async version. You can see this this with the WCF test client.

For example, the following service contract

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

When you fire up the WCF test client you will see 2 methods available

enter image description here

If I change the code to the following

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Task<string> GetDataAsync(int value);
}

public class Service1 : IService1
{
    public async Task<string> GetDataAsync(int value)
    {
        await Task.Delay(value);
        return string.Format("You entered and awaited: {0}", value);
    }
}

I can still call the synchronous string GetData(int) method from my client

enter image description here

Note, you will not be able to use the same interface clientside and serverside anymore to represent the API (and you really shouldn't, the client side interface should have both versions in it. That way the client can decide if it wants to make a blocking call or not). You will still be able to use shared models between them however.

like image 196
Scott Chamberlain Avatar answered Oct 07 '22 00:10

Scott Chamberlain