为什么WCF在关闭客户端应用程序时不会销毁对象而不调用“关闭”客户端的方法?

StackOverflow https://stackoverflow.com/questions/1000355

  •  05-07-2019
  •  | 
  •  

我有一个net tcp WCF服务,如下所示

    [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(){}
    }

当我在客户端创建对象时,如下

AVService client = new AVService();
client.Foo();

调用构造函数,但是当我关闭客户端应用程序而不调用close mehotd时,析构函数没有被调用?为什么?这是否意味着服务对象仍在服务器上运行?

有帮助吗?

解决方案

是 - 如果您没有明确处置您的客户端,服务器将“闲逛”。一段时间(因为你指定了 PerSession 模式)。

它最终会超时(由绑定配置中的 InactivityTimeout 设置指定),并将被销毁。但这可能需要一些时间(几分钟到几个小时,具体取决于您的设置)。

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

因此,最好在关闭应用之前始终处置您的客户

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top