WCF服务不遵守超时配置
问题描述:
我想在WCF服务中测试超时配置。我希望服务根据配置或代码中的设置超时请求。 WCF客户端(例如Web应用程序)具有与预期类似的配置超时,但我希望服务本身遵守设置和超时。WCF服务不遵守超时配置
以几种方式对此进行了测试。
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.config超时指客户操作。你正在做的是设置你的Web服务对你的客户的态度。
根据您的配置,这意味着如果您的客户端需要10秒钟以上的时间,两个请求到您的Web服务,您的客户端将得到一个超时异常,因为您的Web服务会说“嘿!你超出了我的web.config中的超时时间,你迟到了,我不会回答!”。
在客户端的配置中设置相同的超时时间并重复实验,延迟GetData的响应:然后您的客户端将抛出异常并放弃Web服务的响应。
现在,如果您正在寻找设置超时服务端的方法,以便您的服务在超过时间限制时停止处理请求... you will have to implement it yourself。
任何人都有机会看到这个? – Balwant