【发布时间】:2014-04-12 21:19:08
【问题描述】:
我试图通过以下代码了解 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();
请注意:
- 在服务器端设置的 receiveTimeout 属性为 5 秒。
- 在返回数据之前,我在服务器端的 GetData() 方法中等待了 20 秒。
- 我在客户端等待 10 秒,然后再发送另一个请求。
应用程序运行良好,没有任何异常。理想情况下,在我看来,它应该抛出一个异常(根据我对接收超时的 MSDN 定义的理解)。
有人想吗?
谢谢!
【问题讨论】:
标签: wcf wcf-binding