我有一个类库,我正在其上调用WCF服务。此服务大约需要 3 分钟才能完成。我正在使用Windows Forms,并在httpRuntic元素中向App.Config添加了executionTimeout。尽管如此,它不会等到完成交易。如何为通话设置更长的等待时间?
我已经在我的Windows窗体应用程序的App.Config中添加了下面的代码
<system.web>
<compilation debug="false"/>
<httpRuntime executionTimeout="360" />
</system.web>
我在按钮单击中调用我的服务,如下所示。
Registration rObj = new Registration("http://x.x.x.x:1010/Service.svc");
RegInfo sObj = rObj.ValidateRegistration("1234");
MessageBox.Show(sObj.bIsRegistered.ToString());
但是当请求需要很长时间才能响应时,上述调用 TimeOut。
您需要在 WCF 应用程序以及 Windows 窗体应用程序上设置超时
1) WCF应用程序:请在web.config中添加以下更改
=> 第一次更改
<bindings>
<basicHttpBinding>
<binding name="BasicHttpsBindingConfig" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"
receiveTimeout="00:10:00" sendTimeout="00:10:00">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
=> 第二个更改:增加 web.config 中的超时
<configuration>
<system.web>
<httpRuntime executionTimeout="600"/>
</system.web>
</configuration>
2) 窗口表单应用程序示例:
public static void Main()
{
Uri baseAddress = new Uri("http://localhost/MyServer/MyService");
try
{
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService));
WSHttpBinding binding = new WSHttpBinding();
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.CloseTimeout = new TimeSpan(0, 10, 0);
binding.SendTimeout = new TimeSpan(0, 10, 0);
binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
serviceHost.AddServiceEndpoint("ICalculator", binding, baseAddress);
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
catch (CommunicationException ex)
{
// Handle exception ...
}
}