When you are using the
using
statement, all of the objects that can be used here (inside the
using
block) must implement the
IDisposable
[
^] interface. So when you create the block, .NET framework automatically calls the Dispose() function and releases any resource attached to the variables and objects created (specially the one used in the
using
statement).
So in your code, host variable would get
Dispose()
called automatically, once the control hits the closing braces (
}
) of the code block. To give you an overview, your code is similar to this code,
var host = new ServiceHost(typeof (MyService), new Uri(cPar.conn));
host.AddServiceEndpoint(typeof (Itest), cPar.binding,"");
host.Open();
Console.WriteLine("Service running. Press ENTER to stop.");
Console.ReadLine();
((IDisposable)host).Dispose();
The one thing that doesn't get performed is
Close()
automatically, which you have to call yourself to close the connection. Also,
Dispose()
does not call the destructor, it just releases the resources for other objects and methods to use; once object is disposed
.NET framework would call the destructor[^] automatically, you should not call destructors. For more on this please read
this MSDN resource about using
statement[
^] and all other resources attached.