WCF服务未遵守超时配置

4

我正在尝试在WCF服务中测试超时配置。我希望根据配置或代码中的设置,服务能够在规定时间内超时请求。WCF客户端(例如Web应用程序)使用类似的配置时会按预期超时,但我希望服务本身能够遵守设置并超时。

以几种方式进行了测试。

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

<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应用程序上也注意到类似的行为,即使达到配置时间,服务仍不会超时。

--> 欢迎提供任何反馈


有人有机会看一下这个吗? - Balwant
1个回答

0

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

根据你的配置,这意味着如果你的客户端在两个请求之间花费了超过 10 秒的时间,你的客户端将会因为你的 Web 服务说“嘿!你超过了我的 web.config 中的超时时间。你迟到了,我不会回答!”而得到一个超时异常。

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

现在,如果你正在寻找一种在服务端设置超时时间的方法,以便在超过时间限制时停止处理请求... 你将不得不自己实现它


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接