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

Go To StackoverFlow.com


I have a net tcp WCF service as follows

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class AVService : IAVService
        static int _numberofInst = 0;
        public AVService()
            Console.WriteLine("Number of instances "+_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();

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?

2009-06-16 09:08
by Ahmed Said


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).

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

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

2009-06-16 10:24
by marc_s
Good, but inactivityTimeout put into the client configuration file so this means the configurations on the client side will be sent to the server side when the connection established - Ahmed Said 2009-06-16 10:54
You can and should also put it into your server config! Whichever is shorter will "win", if the session is still alive - marc_s 2009-06-16 11:49