How to turn a Standard Service into a Smart Service (Delphi)

A little introduction

In this articles: How to Write a Remoting SDK Server and How to Write a Remoting SDK Client we created a service with two simple methods (Sum and GetServerTime) and a client that can access them. We used the Remoting SDK binary encoding and the HTTP protocol in order for our client to access the server from anywhere on the Internet or from a LAN.

While this work very well, we might face a problem: what if we need non-Remoting clients to access the server?

If your client is a .NET application (running either on the x386 version, Pocket PC, or any port of the Mono implementation) you can use our .Net Client SDK and keep your server exactly the way it is now.

But if you need to have your server accessed by a Java or PHP application you have a problem, since we do not have RO clients for those languages yet.

The solution for this kind of interoperability problems is in a standard encoding format called SOAP (Simple Object Access Protocol). Every major platform available today has means to connect to remote services using the SOAP messaging protocol.

You can find more information about SOAP at http://www.w3c.org or http://msdn.microsoft.com/en-us/library/ms995800.aspx.

We will now make our server capable of being accessed by both binary and SOAP messages at the same time.

Setting up SOAP messaging

In order to add SOAP accessibility to our service, we need to follow a few simple steps.

  • Open the server project and drop a TROSOAPMessage on the main form.
  • Double click on the property Dispatchers of the component ROServer. The dispatchers collection editor will now be displayed.
  • Click Add and select ROSOAPMessage1 from the Message combo box.

This is how everything should look once you are done.

Now save the project and launch it by pressing F9.

You are done! If you launch Internet Explorer and type http://localhost:8099/SOAP you will see the following WSDL document:

Please refer to http://www.w3.org/TR/wsdl for more information on the WSDL specification.

How to make the client use both message types

Remoting clients are capable of accessing services using either Binary or SOAP messaging. In order to make our client access the sample server using SOAP, we need to make a few changes to the original source.

  • Open the client project and drop two TRadioButton components on the main form.
  • Change the caption of the first radio button to Use SOAP Messaging and the second to Use Binary Messaging.
  • Set the property Checked of the first radio button to True.
  • Select the Remoting SDK tab and drop a TROSOAPMessage on the form.

Your client form should now look like this:

Now replace the following code:

myservice := CoTestService.Create(ROBINMessage1, ROWinInetHTTPChannel1);

with this, in both event handlers:

if RadioButton1.Checked then begin
  { SOAP }
  myservice := CoTestService.Create(ROSOAPMessage1, ROWinInetHTTPChannel1);
  ROWinInetHTTPChannel1.TargetURL := 'http://localhost:8099/SOAP';
end
else begin
  { Binary }
  myservice := CoTestService.Create(ROBINMessage1, ROWinInetHTTPChannel1);
  ROWinInetHTTPChannel1.TargetURL := 'http://localhost:8099/BIN';
end;

You are done. You simply instructed the service proxy (the variable myservice) to use different URLs and message formatters to communicate with the server. The code to invoke Sum and GetServerTime didn't change at all.

Conclusions

Now your server application is also accessible via SOAP by any client on any platform.

You could import the WSDL displayed in Internet Explorer from JBuilder, Visual Studio.Net or the clients at http://www.pocketsoap.com.

The big advantage of creating a Smart Service is that you get the best of both worlds: a server that is accessible using an optimized message-format, but that is also available to the rest of the world via SOAP. This combination allows you to maximize both user experience and business potential in one shot, without a single line of code.

In addition, you can see that your client has uniform code regardless of how you are accessing the remote service.