Astronavigator
Astronavigator

Reputation: 2051

How does Delphi web-services work ? ( Adding method in runtime ?? )

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 ?

Upvotes: 3

Views: 2808

Answers (2)

jachguate
jachguate

Reputation: 17203

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:

    try
      FWebNode.Execute(Req, Resp);
    finally
      { Clear Outbound headers }
      FHeadersOutBound.Clear;
    end;          

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.

Upvotes: 1

mjn
mjn

Reputation: 36664

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.

Upvotes: 2

Related Questions