Why WCF does not destroy the object when I close the client application without calling "Close" method on the client side?

destructor, wcf

Solution

Yes - if you don't explicitly dispose of your client, the server will "hang around" for a while (since you specified PerSession mode).

It will eventually time out (specified by the `InactivityTimeout` setting on the binding configuration), and will be destroyed. But that could take some time (several minutes to several hours, depending on your settings).

<bindings>
   <netTcpBinding>
      <binding name="NetTcp_Reliable" receiveTimeout="00:20:00">
         <reliableSession enabled="true" ordered="false" 
                          inactivityTimeout="00:15:00" />
      </binding>
   </netTcpBinding>
</bindings>

Therefore, it is the best practice to always dispose of your client before closing the app.

Problem

I have a net tcp WCF service as follows ``` [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class AVService : IAVService { static int _numberofInst = 0; public AVService() { ++_numberofInst; Console.WriteLine("Number of instances "+_numberofInst); } ~AVService() { --_numberofInst; Console.WriteLine("Number of instances " + _numberofInst); } public void Foo(){} } ``` When I create an object on the client side as follows ``` AVService client = new AVService(); client.Foo(); ``` The constructor is called, but when I close the client application without calling close mehotd, the destructor does not been called? why? does this mean the service object still works on the server?

Original source