如何在Delphi中设置WCF服务调用者的超时时间? [英] How do set the timeout on a wcf service caller in Delphi?
问题描述
我直接调用了iis托管的wcf服务,我是Delphi 2010
I have a straightforward call to a wcf service hosted by iis I'm Delphi 2010
在该服务上调用的操作可能需要几分钟
The operation being called on the service could take several minutes
避免在Delphi中发生超时错误的最佳方法是什么?
What is the best way of avoiding a timeout error in Delphi?
我故意将Thread.Sleep放入WCF服务中强制它等待31秒
I deliberately put a Thread.Sleep inside my WCF Service force it to wait for 31 seconds
30秒后出现错误
项目引发异常类ESOAPHTTPException与消息'句柄处于所请求操作的错误状态-URL:http://10.1.1.4/STC.WcfServices.Host/FlexProcurementService.svc-SOAPAction:http://navsl.stcenergy.com/FlexProcurement/FlexProcurementService/
Project raised exception class ESOAPHTTPException with message 'The handle is in the wrong state for the requested operation - URL:http://10.1.1.4/STC.WcfServices.Host/FlexProcurementService.svc - SOAPAction:http://navsl.stcenergy.com/FlexProcurement/FlexProcurementService/GetPassthroughSummaryGridReportData'.
这原来是Delphi 2010中的一个错误,我已对该补丁应用了此错误,所以现在我得到的错误操作已超时
This turned out to be a bug in Delphi 2010 which I have applied the patch for, so now I get the error operation timed out
function GetFlexProcurementService(const objServiceInfo: TWCFService; UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): FlexProcurementService;
var
RIO: THTTPRIO;
begin
Result := nil;
if (Addr = '') then
begin
if UseWSDL then
Addr := objServiceInfo.WSDL
else
Addr := objServiceInfo.URL;
end;
if HTTPRIO = nil then
RIO := THTTPRIO.Create(nil)
else
RIO := HTTPRIO;
try
Result := (RIO as FlexProcurementService);
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := objServiceInfo.Svc;
RIO.Port := objServiceInfo.Prt;
end else
RIO.URL := Addr;
finally
if (Result = nil) and (HTTPRIO = nil) then
RIO.Free;
end;
end;
Paul
推荐答案
uses wininet;
...
function SetTimeout(const HTTPReqResp: THTTPReqResp; Data: Pointer; NumSecs : integer) : boolean;
var
TimeOut: Integer;
begin
// Sets the receive timeout. i.e. how long to wait to 'receive' the response
TimeOut := (NumSecs * 1000);
try
InternetSetOption(Data, INTERNET_OPTION_RECEIVE_TIMEOUT, Pointer(@TimeOut), SizeOf(TimeOut));
InternetSetOption(Data, INTERNET_OPTION_SEND_TIMEOUT, Pointer(@TimeOut), SizeOf(TimeOut));
except on E:Exception do
raise Exception.Create(Format('Unhandled Exception:[%s] while setting timeout to [%d] - ',[E.ClassName, TimeOut, e.Message]));
end;
end;
在RIO OnBeforePost中:
In the RIO OnBeforePost:
procedure TEETOUpsertWrapper.OnBeforePost(const HTTPReqResp: THTTPReqResp; Data: Pointer); begin
SetTimeout(HTTPReqResp, Data, 5 * 60);
end;
这篇关于如何在Delphi中设置WCF服务调用者的超时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!