我是否需要关闭的.NET服务引用的客户,当我使用它受够了 [英] Do I need to close a .NET service reference client when I'm done using it

查看:144
本文介绍了我是否需要关闭的.NET服务引用的客户,当我使用它受够了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看这里有必要关闭一个.NET服务引用的客户端,当您使用它来完成。几乎所有我所遇到网上的例子似乎没有,但会生成实现IDisposable并且由于它开到服务的连接客户端,我的直觉告诉我,你要当您关闭该连接完成它。

I'm trying to find out if it is neccessary to close a .net service reference client when you are done using it. Almost all of the examples that I have come across on the net don't seem to, but the client that is generated implements IDisposable and since it does open a connection to a service, my intuition tells me you need to close that connection when you are done with it.

下面是一个code示例中,我从<一把拉住href="http://msdn.microsoft.com/en-us/library/bb386386(v=VS.90).aspx">http://msdn.microsoft.com/en-us/library/bb386386(v=VS.90).aspx

Here is a code sample I pulled from http://msdn.microsoft.com/en-us/library/bb386386(v=VS.90).aspx :

private void button1_Click(System.Object sender, System.EventArgs e)
{
    ServiceReference1.Service1Client client = new 
        ServiceReference1.Service1Client();
    string returnString;

    returnString = client.GetData(textBox1.Text);
    label1.Text = returnString;
}

我会认为你至少应该调用client.Close()这个方法结束,并更好的包裹的第一行using语句。我只是想获得一些这方面的反馈意见,找出什么是最好的做法。

I would think that you should at least call client.Close() at the end of this method, and better yet wrap the first line in a using statement. I just wanted to get some feedback on this to find out what the best practices are.

推荐答案

是的,你做什么,但你需要这样做的时候要非常小心。当关闭任何实现<一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx"><$c$c>ICommunicationObject因此存在使处置对象,采取在事件存在的频道上出现错误或故障的时间过多。

Yes, you do, but you need to be very careful when doing so. While closing anything that implements ICommunicationObject the potential exists to cause the disposal of the object to take an excessive amount of time in the event that there is an error or fault on the channel.

由于这个原因,那就是你所说的<一prescribed href="http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.close.aspx"><$c$c>Close法然后调用上的处置方法的IDisposable ,使用数字捕获某些异常类型和调用<一href="http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.abort.aspx"><$c$c>Abort之前,你终于打电话处置

Because of this, it is prescribed that you call the Close method and then call the Dispose method on IDisposable, using a number of catches for certain exception types and calling Abort before you finally call Dispose.

你可以用这种逻辑在一个的IDisposable 的实现,它可以在使用语句中使用,作为我'已经<一个href="http://caspershouse.com/post/Using-IDisposable-on-WCF-Proxies-%28or-any-ICommunicationObject-implementation%29.aspx">outlined在这个问题上的博客帖子。

You can wrap this logic up in an IDisposable implementation which you can use in a using statement, as I've outlined in a blog post on this matter.

这里的关键是创建一个令牌,它实现的IDisposable ,然后在实施中,调用关闭,抓有关例外,叫中止(如有必要),然后调用处置

The key here is to create a token that implements IDisposable and then in that implementation, call Close, catch the relevant exceptions, call Abort (if necessary) and then call Dispose.

这是实现为它返回一个扩展方法的的IDisposable 上从而使您可以使用它在一个使用语句。

This is implemented as an extension method which returns an IDisposable on it which in turn allows you to use it in a using statement.

这篇关于我是否需要关闭的.NET服务引用的客户,当我使用它受够了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆