Abstract: This is the 2nd article that shows how to build a stand-alone Web service using Indy and Delphi 7
Originally published Borland Developer Network.

Copyright 2003 by Serge Dosyukov

Second article from “Distributed Information Systems. From A to Z” series.
If you would like to find other articles, there is a list that will lead you:

This article explains how to fit Indy into Delphi 7’s Web services (SOAP) support. For more on creating Web services, please refer to Nick Hodges’s excellent Borland Community article, Shakespeare on the Web.

One way or another one day you decide to build a server that will be your standalone HTTP server and act as Web-service. One of the reasons could be to create SOAP based Application server for your n-tier Delphi application.

Introduction

Delphi Help provides great step-by-step instruction on how to create Web service, and MIDAS Server (COM, DCOM model), but  there is no information on how to create standalone MIDAS n-tier application based on SOAP protocol Almost

There was a great article by Dave Nottage. This article gave a great idea of how to create a web service in Delphi 6 with SOAP support and the possibility to publish SOAP Datamodule interfaces, so you can build your MIDAS n-tier system.

Since Borland introduced Delphi 7 and the new Indy, we have had native support for such functionality.

Native – yes, documented – no.

Recent posts in Borland newsgroups and searches on google gave us some ideas on how to convert existing code from Delphi 6 to a Delphi 7. But step by step.

The Idea

This is article is the first part out of three. It describes the basis.

The second and third parts coming soon, there we will discuss some problems and how to solve them.

Let’s start.

We are going to create an application that will support the next functionality:

  • be a standalone HTTP server
  • use Indy as a platform
  • support SOAP publication
  • be able to publish SOAP DataModules so you can make your n-tier server SOAP/HTML based.

HTTP server and SOAP

Everybody knows Indy and has used THTTPServer components before. It is easy to drop this component on your application form, but how to make it support SOAP? If you look in “C:\Program Files\Borland\Delphi7\Source\Indy\” folder you can find file IdHTTPWebBrokerBridge.pas. This is what we looking for.

This file is not a part of Indy runtime so you will need to include it in your project as a regular project file. (You also will need IdCompilerDefines.inc to be able to compile your project.). So lets copy them into your project folder. For speed issues, we might make some modifications in a code so lets keep it separate from the Indy distribution source.

Here is an implementation of the replacement of the THTTPServer component extended to support SOAP packets called TIdHTTPWebBrokerBridge. It is a class inherited from TCustomHTTPServer and supported basic request binding.

Because it is not a component available on your palette, you will need to create it as a regular object at runtime in your code.

You can use it exactly the same way you use regular THTTPServer except few things you need to do in addition to make it works with SOAP.

But before we do this let’s prepare some code to use.

WebBroker and Indy

If you have ever created a Web Service before you know we will use WebBroker. Delphi 7 as well as Delphi 6 uses WebBroker architecture to support SOAP.

So you need to create TWebModule and put three components on it: THTTPSoapDispatcher, THTTPSoapPascalInvoker, and TWSDLHTMLPublish – all available on the WebServices tab in the component palette. Link SOAPDispatcher with SOAPPascalInvoker and the form is ready. So finally you will have something like this (uWebModule.pas unit):

You do not need to modify or implement any custom code for this form, so let’s live it as is.

WebModule and Indy

And there is the next part of the code you need to put in your HTTPServer implementation.

As you can see TIdHTTPWebBrokerBridge has a method called RegisterWebModuleClass which allows you to register your WebModule and make it available for the server.

So let’s just call fServer.RegisterWebModuleClass(TwmSOAPIndy) after we created a server object fServer.

Note: In standard implementation of TIdHTTPWebBrokerBridge TwmSOAPIndy object will be created each time when the request came in. Apparently, there is no need for it, so in my version, I modified a class to have creation persistent while the Server object exists. Check class implementation for detail.

“Ready or not, there I am”

“So what next?”, will you ask.

Almost nothing, the server is ready.

You need only add your TSoapDataModule and business logic implementation and the server will be ready completely.

Service? Easy.

Because in our example application is just a regular application you can easily convert it to a service in a regular manner.

No special work should be done.
Couple of things you have to remember:

  • If your DB connections use a trusted connection you have to remember what service has a special account it used to log in to a database server.
  • If you use file storage on your local drive, you need to specify the full path. Service is a special type of program, it does not know anything about the current folder. You can use ExtractFilePath(Forms.Application.EXEName) to get the current folder in such a situation or use a registry to store RootPath for your application.

You can use a user account by changing what account service uses to start.

Example

Here is an example application that shows the result of our work.

It is a standalone server with SOAP support. It has one SOAP interface and CDS available for client application via the SOAP/MIDAS interface.

On the server side, we show the count of database operations performed on the server.

If you click the “Hello” button you will see a result of the call remote interface.

Resources

There is some link that I found helpful…
Creating multi-tier information systems using MIDAS – an old article, but it provides basic information for understanding how N-tier applications could be built using MIDAS in Delphi 5.
WebSnap and Web Services hand-in-hand
Dr. Bob!!! Great Bob Swart!!! Bob Swart’s Delphi Artikelen MIDAS 2 N-Tier Ontwikkelingen
WebSnap Custom Adapter Source Generator Getting started with WebSnap in Delphi 6 Enterprise – by John Kaster