开发者

How to close a WCF service cleanly without cluttering your code?

Reading the accepted answer to this question, apparently the right way to use a WCF service is l开发者_如何学Cike this (copied with some modification)

// 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();
}

However, if my program use the service many times, this will clutter my code a lot. What is the clean way to do this without cluttering my code? Some ideas using lambda expression come to my mind, but so far they don't feel quite clean.


The post you referenced has an Answer which references a post by Marc Gravell which uses an extension method so your WCF calls can be made to look something like this:

using (var client = new Proxy().Wrap()) {
    client.BaseObject.SomeMethod();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜