简体   繁体   English

WCF-确定会话在服务器端何时结束

[英]WCF - Determining when session ends on the server side

In the project I'm working on, we have several services implemented using WCF . 在我正在从事的项目中,我们有一些使用WCF实现的服务。 The situation I'm facing is that some of the services need to know when a session ends, so that it can appropriately update the status of that client. 我面临的情况是某些服务需要知道会话何时结束,以便可以适当地更新该客户端的状态。 Notifying the service when a client gracefully terminates (eg the user closes the application) is easy, however, there are cases where the application might crash, or the client machine might restart, in which case the client won't be able to notify the service about its status. 在客户端正常终止(例如,用户关闭应用程序)时通知服务很容易,但是,在某些情况下,应用程序可能会崩溃,或者客户端计算机可能会重新启动,在这种情况下,客户端将无法通知有关其状态的服务。

Initially, I was thinking about having a timer on the server side, which is triggered once a client connects, and changes the status of that client to "terminated" after, let's say, 1 minute. 最初,我正在考虑在服务器端设置一个计时器,一旦客户端连接,该计时器便会触发,并在例如1分钟后将该客户端的状态更改为“已终止”。 Now the client sends its status every 30 seconds to the service, and the service basically restarts its timer on every request from the client, which means it (hopefully) never changes the status of the client as long as the client is alive. 现在,客户端每30秒将其状态发送给服务,并且服务基本上会根据客户端的每个请求重新启动其计时器,这意味着(希望)只要客户端处于活动状态,它就永远不会更改客户端的状态。

Even though this method is pretty reliable (not fully reliable; what if it takes the client more than 1 minute to send its status?) it's still not the best approach to solving this problem. 即使此方法相当可靠(不是完全可靠;如果客户端发送状态超过一分钟会怎样?),它仍然不是解决此问题的最佳方法。 Note that due to the original design of the system, I cannot implement a duplex service , which would probably make things a lot simpler. 请注意,由于系统的原始设计, 我无法实现双工服务 ,这可能会使事情变得简单得多。 So my question is: Is there a way for the sevice to know when the session ends (ie the connection times out or the client closes the proxy)? 所以我的问题是:服务是否有办法知道会话何时结束(即连接超时或客户端关闭代理)? I came accross this question: WCF: How to find out when a session is ending but the link on the answer seems to be broken. 我遇到了一个问题: WCF:如何确定会话何时结束,但答案上的链接似乎坏了。

Another thing that I'm worried about is; 我担心的另一件事是; they way I'm currently creating my channel proxies is implemented like this: 我目前创建渠道代理的方式是这样实现的:

  internal static TResult ExecuteAndReturn<TProxy, TResult>(Func<TProxy, TResult> delegateToExecute)
  {
      string endpointUri = ServiceEndpoints.GetServiceEndpoint(typeof(TProxy));

      var binding = new WSHttpBinding();
      binding.Security.Mode = SecurityMode.Message;
      binding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

      TResult valueToReturn;

      using (ChannelFactory<TProxy> factory = new ChannelFactory<TProxy>(binding,
                                                                               new EndpointAddress(new Uri(endpointUri), 
                                                                               EndpointIdentity.CreateDnsIdentity(ServiceEndpoints.CertificateName))))
      {
          TProxy proxy = factory.CreateChannel();

          valueToReturn = delegateToExecute(proxy);
      }

      return valueToReturn;
  }

So the channel is closed immediately after the service call is made (since it's in a using block), is that, from a service standpoint, an indication that the session is terminated? 因此,在进行服务调用之后(因为它在using块中),通道立即关闭了,从服务的角度来看,这是否表示会话已终止? If so, should I keep only one instance of each service during application runtime, by using a singleton maybe? 如果是这样,是否应该在应用程序运行时通过使用单例保留每个服务的一个实例? I apologize if the questions seem a little vague, I figured there would be plenty of questions like these but wasn't able to find something similar. 如果问题看起来有点含糊,我深表歉意,我想将会有很多类似的问题,但找不到类似的问题。

Yes, closing the channel terminates the session, but if there is an error of some kind then you are subject to the timeout settings of the service, like this: 是的,关闭通道会终止会话,但是如果出现某种错误,那么您将受到服务超时设置的限制,如下所示:

<binding name="tcpBinding" receiveTimeout="00:00:10" />

This introduces a ten second timeout if an error occurs. 如果发生错误,这将导致十秒钟的超时。

Check out Managing WCF Session Lifetime with IsInitiating and IsTerminating 查看使用IsInitiating和IsTerminate管理WCF会话生存期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM