Reading the accepted answer to this question,显然使用WCF服务的正确方法是这样的(通过一些修改复制)
// create your client
CustomerClient channel = CreateCustomerClient();
try
{
// use it
channel.GetCustomerDetails() ....
(more calls)
// close it
channel.Close();
}
catch(CommunicationException commEx)
{
// a CommunicationException probably indicates something went wrong
// when closing the channel --> abort it
channel.Abort();
}
但是,如果我的程序多次使用该服务,这将使我的代码混乱。如果没有混乱我的代码,干净的方法是什么?我想到了一些使用lambda表达式的想法,但到目前为止他们感觉不太干净。
答案 0 :(得分:1)
您引用的帖子有一个引用a post by Marc Gravell的答案,它使用扩展方法,因此可以使您的WCF调用看起来像这样:
using (var client = new Proxy().Wrap()) {
client.BaseObject.SomeMethod();
}