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

I have net tcp WCF as shown below.

[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, is the destructor not called? What for? Does this mean that the service object is still running on the server?

+4
source share
1 answer

Yes - if you do not explicitly manage your client, the server will freeze for a while (because you specified PerSession ).

In the end, it will expire (specified by the InactivityTimeout parameter in the binding configuration) and will be destroyed. But this may take some time (from 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 best to always delete the client before closing the application .

+3
source

Source: https://habr.com/ru/post/1286278/


All Articles