我正在尝试在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应用程序上也注意到类似的行为,即使达到配置时间,服务仍不会超时。
--> 欢迎提供任何反馈