Building a Web Service from Scratch with Delphi
(Page 1 of 4 )
In the previous article you saw how easy it is to create a client and access web services written in other programming languages. While in the previous article you accessed a web service that was already running, in this article we will be creating our own web service from scratch and create a client to access it.
Two downloadable files are available for this article; you can find them
here and
here.
Building the Server Application
Open Delphi and Select File| New|Other, then go to the "Web Services" tab and select "Soap Server Application."

You should now see this window:

The wizard gives you a number of implementation options about the the type of server you want e.g Isapi, CGI, Apache 1, Apache 2. Select the CGI implementation. CGI is best suited for the purposes of this tutorial, because it will enable you to recompile the service if you want to add more methods or make other changes to it. It compiles an EXE for the service.
Around this time you will be asked to "Create interface for soap module." Select YES to get the standard Web Service interfaces.
You should now see the following screen:

Enter a name for your service and unit (I used MyWebServer). In the code selection section of this window, you have an option to generate sample methods. It will generate example methods just to show you how it is done. Select it if you want, since it will not make any difference to what we want to do.
Two units are now created, one called TMyWebserverImpl and another called IMyWebserverIntf (yours might be different depending on what name you gave them).
- IMyWebServerIntf is the interface for the service and is only modified when you want to add methods and properties.
- TMyWebServerImpl is where the methods of the service are implemented.
Below is what the units look like at creation:
one implementation unit looking like this:
unit MyWebServiceImpl;
interface
uses InvokeRegistry, Types, XSBuiltIns, MyWebServiceIntf;
type
{ TMyWebService }
TMyWebService = class(TInvokableClass, IMyWebService)
public
end;
implementation
initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TMyWebService);
end.
and one Interface unit looking like this:
{ Invokable interface IMyWebService }
unit MyWebServiceIntf;
interface
uses InvokeRegistry, Types, XSBuiltIns;
type
{ Invokable interfaces must derive from IInvokable }
IMyWebService = interface(IInvokable)
['{87217485-DAE6-4970-848B-D649A00F02E3}']
{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
end;
implementation
initialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMyWebService));
end.
Both units with sample methods included looks something like this:
Implementation:
{ Invokable implementation File for TMyWebServer which implements
IMyWebServer }
unit MyWebServerImpl;
interface
uses InvokeRegistry, Types, XSBuiltIns, MyWebServerIntf;
type
{ TMyWebServer }
TMyWebServer = class(TInvokableClass, IMyWebServer)
public
function echoEnum(const Value: TEnumTest): TEnumTest;
stdcall;
function echoDoubleArray(const Value: TDoubleArray):
TDoubleArray; stdcall;
function echoMyEmployee(const Value: TMyEmployee):
TMyEmployee; stdcall;
function echoDouble(const Value: Double): Double; stdcall;
end;
implementation
function TMyWebServer.echoEnum(const Value: TEnumTest): TEnumTest; stdcall;
begin
{ TODO : Implement method echoEnum }
Result := Value;
end;
function TMyWebServer.echoDoubleArray(const Value: TDoubleArray):
TDoubleArray; stdcall;
begin
{ TODO : Implement method echoDoubleArray }
Result := Value;
end;
function TMyWebServer.echoMyEmployee(const Value: TMyEmployee):
TMyEmployee; stdcall;
begin
{ TODO : Implement method echoMyEmployee }
Result := TMyEmployee.Create;
end;
function TMyWebServer.echoDouble(const Value: Double): Double;
stdcall;
begin
{ TODO : Implement method echoDouble }
Result := Value;
end;
initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TMyWebServer);
end.
Interface:
{ Invokable interface IMyWebServer }
unit MyWebServerIntf;
interface
uses InvokeRegistry, Types, XSBuiltIns;
type
TEnumTest = (etNone, etAFew, etSome, etAlot);
TDoubleArray = array of Double;
TMyEmployee = class(TRemotable)
private
FLastName: AnsiString;
FFirstName: AnsiString;
FSalary: Double;
published
property LastName: AnsiString read FLastName write FLastName;
property FirstName: AnsiString read FFirstName write
FFirstName;
property Salary: Double read FSalary write FSalary;
end;
{ Invokable interfaces must derive from IInvokable }
IMyWebServer = interface(IInvokable)
['{7162923C-D2D3-47C8-849D-83FE02939530}']
{ Methods of Invokable interface must not use the default }
{ calling convention; stdcall is recommended }
function echoEnum(const Value: TEnumTest): TEnumTest;
stdcall;
function echoDoubleArray(const Value: TDoubleArray):
TDoubleArray; stdcall;
function echoMyEmployee(const Value: TMyEmployee):
TMyEmployee; stdcall;
function echoDouble(const Value: Double): Double; stdcall;
end;
implementation
initialization
{ Invokable interfaces must be registered }
InvRegistry.RegisterInterface(TypeInfo(IMyWebServer));
end.
We will not be implementing or using these sample methods in our application. It is up to you if you want to include them or not, but from this point onwards we will only be dealing with the units without the sample code.
Next: Writing the Methods >>
More Delphi-Kylix Articles
More By Leidago