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. So it has a timer control, and in the timer there is a Elapsed event:
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:
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.
So my question is two-fold:
- In a windows service, if I instantiate a new class, it gets destroyed as soon as the method ends, right?
- 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?
Overall, what should I do for the service itself when using FileMonitor - should I be destroying it manually, or let garbage collection destroy it?