简体   繁体   English

服务用尽TCP连接

[英]Service running out of TCP connections

I have a windows service, created using c# .net 4.0, which is a monitor for a few things - it has a timer on it, and it has a timer to run it every 5 mins. 我有一个Windows服务,它是使用c#.net 4.0创建的,它是一些事情的监视器-它上面有一个计时器,并且它有一个计时器,每5分钟运行一次。 So it has a timer control, and in the timer there is a Elapsed event: 因此它具有计时器控件,并且计时器中有一个Elapsed事件:

private void Timer_Elapsed(object sender, System.Events.ElapsedEventArgs e)
{
    FileMonitor fileMon = new FileMonitor(url);  

}

Whats happening is that in FileMonitor, its making a connection to a TFS server project, using the TfsTeamProjectCollection class as such: 发生的是,在FileMonitor中,它使用TfsTeamProjectCollection类与TFS服务器项目建立了连接,如下所示:

TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(url, new NetworkCredential( username, password, domain);

What's happening is that I thought that this would automatically close the connection as soon as the method Timer_Elapsed ended, but it doesn't seem to, and the server is running out of TCP connections, and other services can't connect anymore. 发生的事情是,我认为这会在方法Timer_Elapsed结束后立即自动关闭连接,但似乎没有,服务器用尽了TCP连接,其他服务无法再连接了。

So my question is two-fold: 所以我的问题有两个:

  1. In a windows service, if I instantiate a new class, it gets destroyed as soon as the method ends, right? 在Windows服务中,如果我实例化一个新类,则该方法一结束就将其销毁,对吗?
  2. In this case, I've read that TfsTeamProjectCollection doesn't close the connection really: http://social.msdn.microsoft.com/Forums/vstudio/en-US/a0878218-d8dd-4c2a-916f-00bc5b3be2da/tfsteamprojectcollectiondisconnect-missing- - and if thats the case, what is a better way to handle this than what is suggested in that post? 在这种情况下,我读到TfsTeamProjectCollection并不会真正关闭连接: http ://social.msdn.microsoft.com/Forums/vstudio/en-US/a0878218-d8dd-4c2a-916f-00bc5b3be2da/tfsteamprojectcollectiondisconnect- 缺少 -如果是这种情况,那么比该帖子中所建议的解决方案更好的解决方法是什么?

Overall, what should I do for the service itself when using FileMonitor - should I be destroying it manually, or let garbage collection destroy it? 总体而言,使用FileMonitor时应该对服务本身做些什么-应该手动销毁它还是让垃圾回收销毁它?

  1. No it doesn't 不,不是
  2. Either call Disconnect , or better still use the using keyword. 调用Disconnect ,或者最好还是使用using关键字。

Because the class implements IDispose , you can use the using keyword like this: 由于该类实现IDispose ,因此可以使用using关键字,如下所示:

using ( TfsTeamProjectCollection x = New TfsTeamProjectCollection(...) ) {
    ... usage
}

This will close the connection. 这将关闭连接。 To go back to the first point, c# does not implement deterministic destructors like C++. 回到第一点,C#没有实现像C ++这样的确定性析构函数 Instead the object is garbage collected at some point in the future, which you have limited/no control of. 取而代之的是,该对象在将来的某个时候被垃圾回收 ,您对此无法控制。

Because the object has a finalizer the connection it is holding open, will be called when the garbage collector runs. 因为该对象具有终结器 ,所以它保持打开状态的连接将在垃圾收集器运行时被调用。 However, the GC is not running before you're running out of connections. 但是,在连接用尽之前,GC尚未运行。

The TfsTeamProjectCollection object implements IDisposable because it uses unmanaged resources. TfsTeamProjectCollection对象实现IDisposable,因为它使用非托管资源。 As with any other object that implements the IDisposable interface you should call the dispose method when you've finished using it. 与实现IDisposable接口的任何其他对象一样,使用完该对象后,应调用dispose方法。 If you don't then it can hold on to resources. 如果您不这样做,那么它可以保留资源。

The easiest way to do this is to wrap it in a using statement. 最简单的方法是将其包装在using语句中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM