我根据What is the best workaround for the WCF client `using` block issue?
为WCF调用创建了自己的Wrapperpublic 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();
}
}
}
}
我可以用:
来调用它Service<IOrderService>.Use(orderService =>
{
orderService.PlaceOrder(request);
});
如何为异步WCF调用创建它?
我已经尝试过的是:
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();
}
}
}
但它没有用。