了解WCF中的receiveTimeout

时间:2014-04-12 21:19:08

标签: wcf wcf-binding

我想了解" receiveTimeout"通过以下代码在WCF中绑定属性:

服务器端:

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();

请注意:

  1. 服务器端设置的receiveTimeout属性为5秒。
  2. 在返回数据之前,我在服务器端的GetData()方法中等待了20秒。
  3. 我在发送另一个请求之前在客户端等待了10秒钟。
  4. 该应用程序正常运行,没有任何异常。理想情况下,在我看来,它应该抛出一个异常(根据我对MSDN for receiveTimeout定义的理解)。

    有人想到吗?

    谢谢!

1 个答案:

答案 0 :(得分:0)

ReceiveTimeout –服务框架层用于初始化会话空闲超时,该超时控制会话在超时之前可以空闲多长时间。

因此,要获得该行为,您必须使用wsHttpBinding绑定(它支持会话)而不是basicHttpBinding(没有会话,因此没有超时)。

相关问题