2015-02-06 65 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代理是多麼重要。如果你沒有關閉你的代理服務器,並且處於「無限」狀態,那麼在完成你的代理服務器之後,你可能會很好地使用服務器上的資源。