I'm using RabbitMQ.Client for .NET
. When I create connection and I don't stop it by using Dispose
or Close
methods then the connection obviously doesn't stop by itself. But the Main
method is blocking the thread and the console doesn't get closed.
Why is that happening? Why the Finalizer
method of class Foo
doesn't get called too? I thought I could use Finalizers
to close this connection if the user forgets to Dispose
but as far as I can see it's not working this way. Is this a programming error inside this library, or is that expected and if it is, then why is that?
I also tried running GC.Collect()
and stuff like that in various combinations, but it didn't help me.
My main question is - is it possible to pack the IConnection
inside some kind of a supervisor which would Close
it if necessary?
class Foo
{
private IConnection _connection;
public Foo()
{
var factory = new ConnectionFactory
{
HostName = "rancher.off",
Port = 6672
};
_connection = factory.CreateConnection();
}
~Foo()
{
System.Diagnostics.Trace.WriteLine("Foo's destructor is called.");
}
}
class Program
{
static void Main(string[] args)
{
var t = new Foo();
t = null;
// GC.Collect(); <--- It seems this helps! Why? Why isn't GC called after getting out of the Main function?
}
}