2015-02-06 73 views
0

WCF实例模式如何与WCF中的inactivityTimeout相关联?比方说,我有这个在我的web.config结合:WCF实例模式和不活动Timeout

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

如何WCF实例模式,PerCallPerSessionSingle涉及到这个超时?假设我把我的服务归功于PerCall,这个inactivityTimeout只会在我进行服务呼叫的时间范围内有效,并且关闭连接或将其保持“永久”打开状态?

回答

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代理是多么重要。如果你没有关闭你的代理服务器,并且处于“无限”状态,那么在完成你的代理服务器之后,你可能会很好地使用服务器上的资源。