我發現下面的測試對於調查你的問題很有用。總而言之,服務的實例模式似乎對閒置超時沒有任何影響。不活動超時涉及基礎通道在服務器拒絕任何請求之前不活動的時間。
[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代理是多麼重要。如果你沒有關閉你的代理服務器,並且處於「無限」狀態,那麼在完成你的代理服務器之後,你可能會很好地使用服務器上的資源。