【问题标题】:Understanding receiveTimeout in WCF了解 WCF 中的接收超时
【发布时间】: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();

请注意:

  1. 在服务器端设置的 receiveTimeout 属性为 5 秒。
  2. 在返回数据之前,我在服务器端的 GetData() 方法中等待了 20 秒。
  3. 我在客户端等待 10 秒,然后再发送另一个请求。

应用程序运行良好,没有任何异常。理想情况下,在我看来,它应该抛出一个异常(根据我对接收超时的 MSDN 定义的理解)。

有人想吗?

谢谢!

【问题讨论】:

    标签: wcf wcf-binding


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-19
      • 2012-04-22
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多