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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 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 / 8
    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


    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

    - 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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek