tags:

views:

290

answers:

1

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?

+2  A: 

Yes - if you don't explicitly dispose of your client, the server will still "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.

Marc

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
You can and should also put it into your server config! Whichever is shorter will "win", if the session is still alive.
marc_s