原文:http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue/573917#573917
方便使用的一种方式:
public static class MyServiceClientHelp { public static TResult Using<T, TResult>(this T client, Func<T, TResult> work) where T : ICommunicationObject { try { var result = work(client); client.Close(); return result; } catch (Exception e) { client.Abort(); throw; } } }
使用方法:
var result = MyServiceClientHelp.Using (new IntroductionServiceClient(), x => x.IntroduceAsync(new IntroductionRequest { Greeting = "hehhehe", Name = "ly" }).Result ); Console.WriteLine(string.Format("{0} {1}, my name is {2}", result.Greeting, result.ClientName, result.ServiceName));
其他方式自己看原文。