Category Archives: Xamarin

A quick way to get async/await for WCF calls in Xamarin

Just a quick a way to consume WCF services with async/await:

Step 1.
Generate the proxy code with SLSVCUTIL.EXE, there are plenty of examples for that “out there” 🙂

Find your client class and the interface – In this example they are called ProxyClient and Proxy (interface) and the method we are wrapping is called “Call” thant takes a string parameter and returns a string.

Step 2.
Create a small wrapper method, example:

public async Task CallService(string Parameter)
{
ProxyClient client = new ProxyClient(new BasicHttpBiding(), new EndpointAddress("http://host/service"));
var t = Task.Factory.FromAsync(
((Proxy)client.InnerChannel).BeginCall,
((Proxy)client.InnerChannel).EndCall,
Parameter,null);
return await t;
}


Step 3.
Call the method:

result = await CallService("test");

Thats the shortest version I can come up for solving the missing async/await support for WCF services. You should remove the ProxyClient construction to somewhere else depending on your WCF consumption to reuse the client.