SunQuest
 
       Delphi-Kylix
  Home arrow Delphi-Kylix arrow Building a Web Service from Scratch with D...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM developerWorks
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DELPHI-KYLIX

Building a Web Service from Scratch with Delphi
By: Leidago
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-11-06

    Table of Contents:
  • Building a Web Service from Scratch with Delphi
  • Writing the Methods
  • Adding state
  • Testing the service

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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. 

    More Delphi-Kylix Articles
    More By Leidago


       · Web services are a exiting new technology, this article will explain how you can...
     

    DELPHI-KYLIX ARTICLES

    - Client Dataset: Working with Data Packets an...
    - Using the Client Dataset in an N-Tiered Appl...
    - Using the Client Dataset in Two-Tiered Clien...
    - Using the Client Dataset in File-Based Archi...
    - Demystifying the Client Dataset
    - Working with INI Files in Delphi
    - Creating Data Link (UDL) Files in Delphi
    - Looking at the Details for an Invoicing Appl...
    - Invoicing in Delphi: Show Me the Money
    - Saving Images and Binary Files to a Database...
    - Saving Files to a Database using Delphi: Sav...
    - Creating CF Applications and Integrating a S...
    - Using Try and Finally to Help Prevent Memory...
    - The Implementation of an FTP Server
    - FTP Server: The Theory







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway