c# - Why is the Destructor not being called? -
i have interesting scenario class inform entity has been destroyed; however, not doing want too.
the problem
the deconstructor, reason not supposed do.
the question
why destructor not being invoked , make sure necessary clean up.
the code
so here have informer ~
class connection { public const int port = 50000;// can range between 49152 , 65536 //teh constructor public boolean connect() { //setinformation information.id = 545; using (var webserv = new clientsdksoapclient("clientsdksoap")) { continueconnection.waitone(); webserv.clientlogin(information); } return true; } ~connection() { using (var webserv = new clientsdksoapclient("clientsdksoap")) { webserv.clientlogout(information); } } }
additional information
i want web service record if connection class destroyed given reason. when client connecting, works perfectly. web service records every method called it. if call clientlogout explicitly, work.
i aware can implement idisposable; however, object not intended used within lifetime of 1 method. in fact, intended use entire duration of program , failure of object results in failure of entire project. (although suppose main method...)
i need release network connection; however, not in program, in program , unless clientlogout called, won't released.
my research
microsoft says should use deconstructor release of unmanaged resources making explicit reference network connections. ones got quite stumped.
i think should implement dispose
pattern connection class, rather relying on obscure deconstructor metaphor. "canonical" way it.
public class connection : idisposable // <== inherit idisposable interface { public const int port = 50000;// can range between 49152 , 65536 private sometype webserv; // use whatever real type appropriate here. private information information = new information(); // or whatever // real constructor. public connection() { //setinformation information.id = 545; webserv = new clientsdksoapclient("clientsdksoap")) webserv.continueconnection.waitone(); webserv.clientlogin(information); } // implement idisposable interface public void dispose() { webserv.clientlogout(information); } }
and use thusly
using (var connection = new connection()) { // use connection here. }
the client logged out when leave using
block.
Comments
Post a Comment