【发布时间】:2013-07-02 13:14:24
【问题描述】:
我需要与一系列 .Net Web 服务进行交互。目前大约有150个。 由于 delphi 2010 使用 Thttprio 来实现这一点,我试图在客户端创建一个通用代理,可以调用它来创建适当的肥皂服务客户端。 有谁知道如何将 httprio 对象转换为通用接口类型?
谢谢
下面是我尝试使用的代理功能:
class function Web.Proxy<T>(svc: string): T;
var
HTTPRIO : THTTPRIO;
begin
HTTPRIO := THTTPRIO.Create(nil);
HTTPRIO.URL := GetServiceURL(svc);
Result:= HTTPRIO as T; //<-- Fails with "operator not applicable to this operand type"
// Result:= T(HTTPRIO); //<-- also fails, but with "invalid typecast"
end;
这个想法是我可以这样称呼它:
Web.Proxy<AutmobileServiceSoap>('svc.asmx').GetAutomobile(125);
WSDL 导入的 AutmobileServiceSoap 定义如下:
AutmobileServiceSoap = interface(IInvokable)
并且所有 wsdl 导入都有一个函数,它以类似的方式返回 httprio 对象:
function GetAutomobileServiceSoap(UseWSDL: Boolean; Addr: string; HTTPRIO: THTTPRIO): AutomobileServiceSoap;
const
defWSDL = 'http://localhost:8732/Cars.Server/Data/AutomobileService.asmx?WSDL';
defURL = 'http://localhost:8732/Cars.Server/Data/AutomobileService.asmx';
defSvc = 'AutomobileService';
defPrt = 'AutomobileServiceSoap12';
var
RIO: THTTPRIO;
begin
Result := nil;
if (Addr = '') then
begin
if UseWSDL then
Addr := defWSDL
else
Addr := defURL;
end;
if HTTPRIO = nil then
RIO := THTTPRIO.Create(nil)
else
RIO := HTTPRIO;
try
Result := (RIO as AutomobileServiceSoap);
if UseWSDL then
begin
RIO.WSDLLocation := Addr;
RIO.Service := defSvc;
RIO.Port := defPrt;
end else
RIO.URL := Addr;
finally
if (Result = nil) and (HTTPRIO = nil) then
RIO.Free;
end;
end;
【问题讨论】:
标签: delphi generics delphi-2010