【问题标题】:WCF Instance mode and inactivityTimeoutWCF 实例模式和 inactivityTimeout
【发布时间】:2015-02-06 19:24:34
【问题描述】:

WCF 实例模式与 WCF 中的 inactivityTimeout 有什么关系?假设我的 web.config 中有这个绑定:

<wsHttpBinding>
 <binding name="WSHttpBinding" receiveTimeout="infinite">
   <reliableSession inactivityTimeout="infinite" enabled="true" />
 </binding>
</wsHttpBinding>

WCF 实例模式PerCallPerSessionSingle 与此超时有何关系?假设我将我的服务归因于PerCall,这个inactivityTimeout 是否仅在我进行服务调用并关闭连接的时间段内有效,还是会“永远”保持打开状态?

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    我发现以下测试有助于调查您的问题。总之,服务的实例模式似乎与非活动超时无关。不活动超时涉及在服务器拒绝来自它的任何请求之前底层通道不活动的时间。

    [TestClass]
    public class Timeouts
    {
        [TestMethod]
        public void Test()
        {
            Task.Factory.StartNew(() =>
            {
                var serverBinding = new NetTcpBinding();
                serverBinding.ReliableSession.Enabled = true;
                serverBinding.ReliableSession.InactivityTimeout = TimeSpan.FromSeconds(4);
                var host = new ServiceHost(typeof (MyService));
                host.AddServiceEndpoint(typeof (IMyService), serverBinding, "net.tcp://localhost:1234/service");
                host.Open();
            }).Wait();
    
            var clientBinding = new NetTcpBinding();
            clientBinding.ReliableSession.Enabled = true;
    
            var channelFactory = new ChannelFactory<IMyService>(clientBinding, "net.tcp://localhost:1234/service");
    
            var channelOne = channelFactory.CreateChannel();
    
            channelOne.MyMethod();
            Thread.Sleep(TimeSpan.FromSeconds(3));
            channelOne.MyMethod();
    
            (channelOne as ICommunicationObject).Close();
    
            Thread.Sleep(TimeSpan.FromSeconds(5));
    
            var channelTwo = channelFactory.CreateChannel();
    
            channelTwo.MyMethod();
            Thread.Sleep(TimeSpan.FromSeconds(6));
            channelTwo.MyMethod();
    
            (channelTwo as ICommunicationObject).Close();
        }
    
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
        public class MyService : IMyService
        {
            public void MyMethod()
            {
                Console.WriteLine("Hash: " + GetHashCode());
                Console.WriteLine("Session: " + OperationContext.Current.SessionId);
                Console.WriteLine();
            }
        }
    
        [ServiceContract]
        public interface IMyService
        {
            [OperationContract]
            void MyMethod();
        }
    }
    

    我认为这只是强调了始终关闭您的客户对 WCF 代理的重要性。如果您不关闭您的代理,并且“无限”的不活动超时,您很可能会在完成代理后很好地使用服务器上的资源。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      相关资源
      最近更新 更多