Delphi Web 服务如何工作? (在运行时添加方法??)
我已经使用 WSDL 导入器在 Delphi XE 中创建了 Web 服务。 Delphi 为我生成模块 ITransmitter1.pas ITransmitter接口和GetITransmitter函数。
要使用网络服务,我使用:
var Transmitter: ITransmitter;
begin
Transmitter := GetITransmitter(True, '', nil);
Transmitter.Transmit(Memo1.Text, OutXML);
end;
但我看不到方法 Transmit 的任何地方 ...
在 ITransmitter.pas 中我看到:
InvRegistry.RegisterInterface(TypeInfo(ITransmitter), 'urn:TransmitterIntf-ITransmitter', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ITransmitter), 'urn:TransmitterIntf-ITransmitter#Transmit');
如果我评论这一行,我会收到“接口不支持”错误。 正如我在这里看到的,delphi 正在运行时添加方法! 它是如何运作的?我可以在运行时将方法添加到我自己的类中吗?
I've created web-service in Delphi XE using WSDL importer.
Delphi generated for me module ITransmitter1.pas with
ITransmitter interface and GetITransmitter function.
To use webservice i use:
var Transmitter: ITransmitter;
begin
Transmitter := GetITransmitter(True, '', nil);
Transmitter.Transmit(Memo1.Text, OutXML);
end;
But i cant see anywhere body of method Transmit ...
In ITransmitter.pas i see:
InvRegistry.RegisterInterface(TypeInfo(ITransmitter), 'urn:TransmitterIntf-ITransmitter', 'utf-8');
InvRegistry.RegisterDefaultSOAPAction(TypeInfo(ITransmitter), 'urn:TransmitterIntf-ITransmitter#Transmit');
If i comment this lines i get "interface not supported" error.
As i see here delphi is adding method in RunTime !
How does it work ? Can i add method in runtime to my own class ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 WSDL 导入器创建了 Web 服务客户端,则生成的客户端代码将调用服务器上的方法。所以实际上,方法“body”(代码)位于Web服务服务器上。
Delphi 基于 WSDL 生成 Soap 请求,并在后台使用 RTTI(内省)以 XML 形式生成 Web 服务调用的参数等。该 XML 被发送到服务器,服务器执行方法实现并发回 Soap 响应。
如果您创建 Web 服务服务器,情况则相反,在这种情况下,Delphi 应用程序当然需要实现所有方法体。
If you created a web service client with the WSDL importer, the generated client code will invoke a method on the server. So in fact, the method 'body' (code) is on the web service server.
Delphi generates a Soap request based on the WSDL, and behind the scenes RTTI (introspection) is used to generate parameters etc. of the web service call as XML. This XML is sent to the server, which executes the method implementation and sends back a Soap response.
Things are opposite if you create the web service server, in this case the Delphi application of course needs to implement all method bodies.
实际上,您正在调用接口中定义的方法,该方法又继承自 System.pas 中声明的 IInvokable。
如果检查源代码,您会注意到项目中没有本地对象实现您正在调用的 IInvokable 接口,这是因为该方法是在服务器中远程执行的。
在此之前,有一些 pascal 代码用于创建到服务器的正确 SOAP 请求,发送该请求,然后等待并解释服务器响应,请考虑此实现细节。如果您有兴趣更多地了解其工作原理,请启用“use debug .dcus”编译器选项,以便您可以在 VCL/RTL 内部进行调试。
然后,像往常一样,使用 StepInto (F7) 命令要求调试器逐步执行 Transmit 方法...在 TRIO.GenericStub 方法中进行一些汇编之后,您将到达数据包所在的 TRIO.Generic 方法准备并发送。
对于我用来编写此响应的 btSOAP 绑定,相关部分从 Rio.pas 单元中的第 943 行开始:
THTTPReqResp.Execute 然后使用 wininet.dll 函数与服务器执行连接、发送和接收信息。
您可以深入探讨某些级别...您想要走多远将取决于您的兴趣,并且大量细节远远超出了我在这里回答的范围...请随时发布更多具体问题 。
我不确定,但细节可能会在 Delphi 版本之间发生变化...我现在正在使用 Delphi XE
You're in fact calling a method defined in a Interface, which in turn inherits from IInvokable, declared in System.pas.
If you check your source code you'll note that no local object in your project implements the IInvokable interface you're calling, that's because that method is remotely executed in the server.
Before that occurs, there's some pascal code used to create a proper SOAP request to the server, send it and then wait and interpret the server response, consider this implementation details. If you're interested in know a bit more how this works, enable the "use debug .dcus" compiler option, so you can debug inside the VCL/RTL.
Then, as usual, use the StepInto (F7) command to ask the debugger to execute the Transmit method step by step... after some assembler in the TRIO.GenericStub method you'll get to the TRIO.Generic method where the packet is prepared and sent.
For a btSOAP binding I'm using to write this response, the relevant part starts at line 943 in the Rio.pas unit:
THTTPReqResp.Execute then uses wininet.dll functions to perform the connection, send and receive of information with the server using.
There are some levels you can go deep with this... how far you want to go will depend on your interests and the great amount of details are far beyond the scope of my answer here... feel free to post more questions with specific tings you're interested in.
I'm not sure about, but details can change between Delphi versions... I'm using Delphi XE right now.