我发现下面的测试对于调查你的问题很有用。总而言之,服务的实例模式似乎对闲置超时没有任何影响。不活动超时涉及基础通道在服务器拒绝任何请求之前不活动的时间。
[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代理是多么重要。如果你没有关闭你的代理服务器,并且处于“无限”状态,那么在完成你的代理服务器之后,你可能会很好地使用服务器上的资源。