我试图通过以下代码了解WCF中的“receiveTimeout”绑定属性:
服务器端:
ServiceHost serviceHostForStrong = new ServiceHost(typeof(Service), new Uri("http://localhost:8887/Service"));
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
basicHttpBinding.ReceiveTimeout = new TimeSpan(0, 0, 5);
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/");
ServiceEndpoint serviceEndpoint =
new ServiceEndpoint(ContractDescription.GetContract(typeof(IServiceContract)),
basicHttpBinding,
endpointAddress);
serviceHostForStrong.AddServiceEndpoint(serviceEndpoint);
serviceHostForStrong.Open();
Console.WriteLine("Service is running....Press any key to exit");
Console.ReadKey();
客户端:
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8887/Service/");
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
ChannelFactory<IServiceContract> channelFactory = new ChannelFactory<IServiceContract>(basicHttpBinding, endpointAddress);
IServiceContract proxy = channelFactory.CreateChannel(endpointAddress);
Console.WriteLine("Data received: " + proxy.GetData());
Thread.Sleep(new TimeSpan(0, 0, 10));
Console.WriteLine("Data received after 10 seconds: " + proxy.GetData());
Console.WriteLine("Press any key to exit.....");
Console.ReadKey();
请注意:
该应用程序正常运行,没有任何例外理想情况下,在我看来,它应该抛出一个异常(根据我从MSDN的receiveTimeout定义的理解)。
想什么?
谢谢!
ReceiveTimeout - 由服务框架层用于初始化会话空闲超时,该超时控制会话在超时之前可以空闲多长时间。
因此,要获得该行为,您必须使用wsHttpBinding绑定(它支持会话)而不是basicHttpBinding(没有会话,因此不会收到超时)。