I've created my own Wrapper for WCF calls according to What is the best workaround for the WCF client `using` block issue?
public delegate void UseServiceDelegate<T>(T proxy);
public static class Service<T>
{
public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>("");
public static void Use(UseServiceDelegate<T> codeBlock)
{
IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
bool success = false;
try
{
codeBlock((T)proxy);
proxy.Close();
success = true;
}
finally
{
if (!success)
{
proxy.Abort();
}
}
}
}
I can call it with:
Service<IOrderService>.Use(orderService =>
{
orderService.PlaceOrder(request);
});
How can I make this for asynchronous WCF calls?
What I've already tried is:
public delegate Task UseAsyncServiceDelegate<T>(T proxy);
public static async Task UseAsync(UseAsyncServiceDelegate<T> codeBlock)
{
IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
bool success = false;
try
{
await codeBlock((T)proxy);
proxy.Close();
success = true;
}
finally
{
if (!success)
{
proxy.Abort();
}
}
}
But it is not working.