WCF实例模式如何与WCF中的inactivityTimeout相关?假设我在web.config中有这个绑定:
<wsHttpBinding>
<binding name="WSHttpBinding" receiveTimeout="infinite">
<reliableSession inactivityTimeout="infinite" enabled="true" />
</binding>
</wsHttpBinding>
WCF实例模式PerCall
,PerSession
,Single
如何与此超时相关?假设我将我的服务归因于PerCall
,这个inactivityTimeout
仅在我进行服务调用的时间段内有效,并关闭连接还是会“永久”保持打开状态?
答案 0 :(得分: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代理的重要性。如果您没有关闭代理服务器,并且不活动超时为“无限”,则在完成代理服务器后,您最终可能会在服务器上使用资源。