【问题标题】:WCF Service not honoring timeout configurationWCF 服务不遵守超时配置
【发布时间】:2018-03-30 10:30:07
【问题描述】:

我正在尝试在 WCF 服务中测试超时配置。我希望服务根据配置或代码中的设置使请求超时。 WCF 客户端(即 Web 应用程序示例)具有与预期类似的配置超时,但我希望服务本身遵守设置和超时。

以多种方式对此进行了测试。

1.) IIS 托管的 WCF 服务在 web.config 中列出了超时时间

<bindings>
   <basicHttpBinding>
     <binding name="Service1Binding" closeTimeout="00:00:10" openTimeout="00:00:10" receiveTimeout="00:00:10" sendTimeout="00:00:10">
      <security mode="TransportWithMessageCredential">
        <message clientCredentialType="UserName"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>
<services>
  <service name="Service1">
    <endpoint address="Service1" binding="basicHttpBinding" bindingConfiguration="Service1Binding" name="IService1" contract="TestWcfTimeout.IService1" />
  </service>
</services>

2.) 代码中列出超时的自托管 WCF 服务

            using (var host = new ServiceHost(typeof(Service1), baseAddress))
            {
                var smb = new ServiceMetadataBehavior{ HttpGetEnabled = true, MetadataExporter = { PolicyVersion = PolicyVersion.Policy15 }};

                var endpoint = host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(), baseAddress);
                endpoint.Binding.OpenTimeout = new TimeSpan(00, 00, 00, 10);
                endpoint.Binding.CloseTimeout = new TimeSpan(00, 00, 00, 10);
                endpoint.Binding.SendTimeout = new TimeSpan(00, 00, 00, 10);
                endpoint.Binding.ReceiveTimeout = new TimeSpan(00, 00, 00, 10);
                host.Description.Behaviors.Add(smb);
                host.Open();

                Console.WriteLine("The service is ready at {0}", baseAddress);
                Console.WriteLine("Press <Enter> to stop the service.");
                Console.ReadLine();

                host.Close();
            }

     [ServiceContract]
     public interface IService1
     {
        [OperationContract]
        string GetData(int value);
     }

使用 SoapUI 客户端对此进行了测试。确保 GetData 方法完成执行所需的时间比配置的超时时间长。因此服务可以使请求超时。

--> 但服务不遵守超时设置,即使达到超时设置,客户端也会收到 wcf 响应对象。有什么想法吗?

注意到 Web 应用程序上的类似行为,即使在达到配置时间后服务也不会超时。

-->感谢任何反馈

【问题讨论】:

  • 有人有机会看看这个吗?

标签: web-services wcf


【解决方案1】:

您的 web.config 超时是指客户端的操作。您所做的是设置 Web 服务对客户的态度。

根据您的配置,这意味着如果您的客户端在例如向您的 Web 服务发出两次请求,您的客户端将收到超时异常,因为您的 Web 服务会说“嘿!您超出了我的 web.config 中的超时时间。您迟到了,我不会回答!”。

在您的客户端配置中设置相同的超时时间并重复您的实验,延迟 GetData 的响应:然后您的客户端将抛出异常并丢弃您的 Web 服务的响应。

现在,如果您正在寻找一种设置超时服务端的方法,以便您的服务在超过时间限制时停止处理请求...you will have to implement it yourself

【讨论】:

    猜你喜欢
    • 2014-12-27
    • 2013-03-24
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2015-02-04
    • 2020-04-04
    相关资源
    最近更新 更多