6

在 PerSession 中,如何让服务上的 Dispose() 触发?在下面的代码中 Dispose() 没有被调用。当我调用 .Close() 或让会话超时时都不会。

如果我将服务更改为 PerCall,则调用 Dispose()(每个方法调用)。使用 PerSession 我得到一个会话(用 serviceStartTime 测试)。

服务

[ServiceBehavior (InstanceContextMode=InstanceContextMode.PerSession)]
public class MagicEightBallService : IEightBall, IDisposable
{
    private DateTime serviceStartTime;
    public void Dispose()
    {
        Console.WriteLine("Eightball dispose ... " + OperationContext.Current.SessionId.ToString());
    }
    public MagicEightBallService()
    {
        serviceStartTime = DateTime.Now;
        Console.WriteLine("Eightball awaits your question " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString());
    }
    public string ObtainAnswerToQuestion(string userQuestion)
    {
        return "maybe " + OperationContext.Current.SessionId.ToString() + " " + serviceStartTime.ToLongTimeString();
    }

客户

    using (EightBallClient ball = new EightBallClient())
    {    
        while (true)
        {
            Console.Write("Your question: ");
            string question = Console.ReadLine();
            if (string.IsNullOrEmpty(question)) break;
            try
            {
                string answer = ball.ObtainAnswerToQuestion(question);
                Console.WriteLine("8-ball says: {0}", answer);
            }
            catch (Exception Ex)
            {
                Console.WriteLine("ball.ObtainAnswerToQuestion exception " + Ex.Message);
            }               
        }
        ball.Close();
     }

服务合约

[ServiceContract (SessionMode = SessionMode.Required)]
public interface IEightBall
{
    [OperationContract]
    string ObtainAnswerToQuestion(string userQuestion);

    [OperationContract]
    sDoc GetSdoc(int sID);

    DateTime CurDateTime();
}

主持人

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ISampleService" 
                 closeTimeout="00:01:00" openTimeout="00:01:00" 
                 receiveTimeout="00:10:00" sendTimeout="00:10:00">
          <security mode="Message" />
          <reliableSession ordered="true"
                   inactivityTimeout="00:10:00"
                   enabled="true" />
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="MajicEightBallServiceLib.MagicEightBallService"
               behaviorConfiguration="EightBallServiceMEXBehavior" >
        <endpoint address=""
                  binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ISampleService"
                  contract="MajicEightBallServiceLib.IEightBall">
        </endpoint>
        <endpoint address="mex"
                  binding ="mexHttpBinding"
                  contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8000/MagicEightBallService"/>
          </baseAddresses>
        </host>             
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="EightBallServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

namespace MagicEightBallServiceHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("**** Console Based WCF Host *****");

            using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
            {
                serviceHost.Open();
                Console.WriteLine("The service is running");
                Console.ReadLine();
            }
        }
    }
}
4

1 回答 1

10

Dispose()方法将被解雇。唯一的问题是“什么时候?”。

该问题的答案取决于服务配置。

有几种可能的情况:

  1. 绑定不支持会话
  2. 正常会话
  3. 可靠的会话

Dispose()PerSession在上下文模式下关闭会话时触发。所以我们需要检查会话在不同场景下的存活时间。

对于某些配置(例如 default BasicHttpBinding),会话根本没有启动。在无会话配置 PerCallPerSession上下文模式没有区别的情况下, Dispose方法将在您的主方法执行后很快被调用。

何时启用Session它可以由客户端或超时显式关闭。通常它是由客户端控制的。客户端在第一次调用服务之前启动会话,并在客户端对象关闭时关闭它。

ServiceClient proxy = new ServiceClient();
Console.WriteLine(proxy.GetData(123));
proxy.Close();  

proxy.Close()上面的方法关闭服务器中的会话,然后执行Dispose()

会话管理是一个很大的性能驱动因素,因为它需要在客户端和服务器之间执行额外的调用。

所以通常Dispose会在客户端想要关闭会话时调用。

如果客户端由于任何原因没有关闭会话,它将在一段时间后被服务主机关闭。该时间段由Binding.ReceiveTimeout属性控制。该属性的默认值为 10 分钟。

Dispose()如果在 10 分钟内没有人向具有特定会话 ID 的服务器发送请求,会话将被关闭并(被触发)。receiveTimeout可以通过在 web.config 中设置一些较短的值来更改此默认超时。

<wsHttpBinding>
  <binding name="wsHttpEndpointBinding" receiveTimeout="00:00:05">
  </binding>
</wsHttpBinding> 

启用可靠会话时,还会额外检查 ReliableSession.InactivityTimeout。它也默认为 10 分钟。

它在自托管和 IIS 托管服务中按预期工作。

尝试如下更新您的客户端代码进行测试:

using (EightBallClient ball = new EightBallClient())
{    
    ball.ObtainAnswerToQuestion("test");
    ball.Close();
} 
于 2012-06-15T17:12:15.830 回答