WCF Dispose()不使用InstanceContectMode = PerSession调用

pap*_*zzo 6 .net wcf

在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();
    }
Run Code Online (Sandbox Code Playgroud)

客户

    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();
     }
Run Code Online (Sandbox Code Playgroud)

服务合约

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

    [OperationContract]
    sDoc GetSdoc(int sID);

    DateTime CurDateTime();
}
Run Code Online (Sandbox Code Playgroud)

主办

<?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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Dmi*_*ski 10

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

对该问题的回答取决于服务配置.

有几种可能的情况:

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

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

对于某些配置(例如默认BasicHttpBinding),会话根本不会启动.在会议上,更少的配置情况 PerCallPerSession情境模式有没有差异和 Dispose方法将很快后您的主要方法执行调用.

Session启用了可通过客户端或超时明确关闭.通常它由客户控制.客户端在首次调用服务之前启动会话,并在关闭Client对象时关闭它.

ServiceClient proxy = new ServiceClient();
Console.WriteLine(proxy.GetData(123));
proxy.Close();  
Run Code Online (Sandbox Code Playgroud)

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

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

因此,通常Dispose在客户端要关闭会话时调用.

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

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

<wsHttpBinding>
  <binding name="wsHttpEndpointBinding" receiveTimeout="00:00:05">
  </binding>
</wsHttpBinding> 
Run Code Online (Sandbox Code Playgroud)

ReliableSession.InactivityTimeout当启用了可靠的会话额外检查.它也默认为10分钟.

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

尝试按如下方式更新客户端代码以进行测试:

using (EightBallClient ball = new EightBallClient())
{    
    ball.ObtainAnswerToQuestion("test");
    ball.Close();
} 
Run Code Online (Sandbox Code Playgroud)