Home arrow Delphi-Kylix arrow Building a Web Service from Scratch with Delphi
DELPHI-KYLIX

Building a Web Service from Scratch with Delphi


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.

Author Info:
By: Leidago
Rating: 5 stars5 stars5 stars5 stars5 stars / 17
November 06, 2006
TABLE OF CONTENTS:
  1. · Building a Web Service from Scratch with Delphi
  2. · Writing the Methods
  3. · Adding state
  4. · Testing the service

print this article
SEARCH DEVARTICLES

Building a Web Service from Scratch with Delphi
(Page 1 of 4 )


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. 


blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials