Home arrow Delphi-Kylix arrow Page 3 - 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 / 10
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

TOOLS YOU CAN USE

advertisement
Building a Web Service from Scratch with Delphi - Adding state
(Page 3 of 4 )

 

Now there's just one more thing we need to do before testing our service. CGI applications by their very nature do not have state, in other words they do not remember or hold any data. So for example, if we now run this service and enter a text for the setTxt procedure, the text will not be remembered by the application, so we need to find a way to store the data somewhere. To solve this problem, we are going to create two more methods that will help in this regard. In the implementation units' "public" section add the following two methods:

Constructor Create; Override;

Destructor Destroy; Override;

Then in the implementation section add the following code:

constructor TMyWebServer.Create;
Var 
  f : textfile;
  buff:string;
begin 
  inherited;
  AssignFile(f, 'c:ATxtfile.txt');
 Reset(f);
 ReadLn(f, buff);
TheTxt:=buff;
 closefile(f);
end;
destructor TMyWebServer.Destroy;
Var
 f : textfile;
begin
  AssignFile(f, 'c:ATxtfile.txt');
 Rewrite(f);
 WriteLn(f, TheTxt);
 CloseFile(f);
  inherited;
end;

Basically what these two methods do is write the text entered by the user to a text file called "Atxtfile." So all that the application should do is read the text from this file when the getTxt method is called.

After all these changes your two units should look something like this:

Interface unit:

{ Invokable interface IMyWebServer }
unit MyWebServerIntf;
interface
uses InvokeRegistry, Types, XSBuiltIns;
type
  { Invokable interfaces must derive from IInvokable }
  IMyWebServer = interface(IInvokable)
  ['{20BC8DE1-3A20-462D-AAF0-7B96FFDD72CD}']
    { Methods of Invokable interface must not use the default }
    { calling convention; stdcall is recommended }
    Procedure SetTxt(Const AValue : String); stdcall;
    Function GetTxt : String; stdcall;
  end;
implementation
initialization
  { Invokable interfaces must be registered }
  InvRegistry.RegisterInterface(TypeInfo(IMyWebServer));
end.

Implementation Unit

{ Invokable implementation File for TMyWebServer which implements IMyWebServer }
unit MyWebServerImpl;
interface
uses InvokeRegistry, Types, XSBuiltIns, MyWebServerIntf,IniFiles;
type
  { TMyWebServer }
  TMyWebServer = class(TInvokableClass, IMyWebServer)
  Private 
  TheTxt : String;
  public
    Procedure SetTxt(Const AValue : String); stdcall;
    Function GetTxt : String; stdcall;
   Constructor Create; Override;
    Destructor Destroy; Override;
  end;
implementation
constructor TMyWebServer.Create;
Var 
  f : textfile;
  buff,ATxt:string;
begin 
  inherited;
  AssignFile(f, 'ATxtfile.txt');
 Reset(f);
 while not EOF(f) do begin
 ReadLn(f, buff);
TheTxt:=ATxt;
end;
closefile(f);
end;
destructor TMyWebServer.Destroy;
Var
 f : textfile;
begin
  AssignFile(f, 'ATxtfile.txt');
 Append(f);
 WriteLn(f, TheTxt);
 CloseFile(f);
  inherited;
end;
function TMyWebServer.GetTxt: String;
begin 
  Result := TheTxt;
end;
procedure TMyWebServer.SetTxt(const AValue: String);
begin 
 TheTxt := AValue;
end;
Initialization
  { Invokable classes must be registered }
  InvRegistry.RegisterInvokableClass(TMyWebServer);
end.

Save all your project files in one directory, including the interface and implementation units. Then in the Delphi IDE go to Projects, then options, and then set the output directory to "X:inetpubwwwrootcgi-bin." "X" is for whatever drive name your web server root is located on. The cgi-bin directory is not automatically included in the webroot for those of us who run IIS, so create it and give it execute rights. We set the output directory to the cgi-bin directory because we won't have to copy our exe to the cgi-bin directory every time we make changes to our application. The exe will automatically be placed in that directory when we recompile it. Unlike most other web services, our web service does not need an Internet connection, but requires a web server.

Compile and run the service. An EXE file should now be created; to verify, check your cgi-bin directory. Then open up your browser and enter "http://localhost/cgi-bin/thenameofyourproject.exe" You should see something like this in your browser:

 

There you can see our two methods listed and also a link to the web service WSDL file, which we will need to create a client. Click on this link and you should see something like this:

If you are using IE, go to File|Save As... and save the file.  Call it whatever you like (I saved mine as IMyWebServer), as long as its extension is xml. Also make sure to save the file in your project directory and not your cgi-bin dir. This way we know where to look for the WSDL file.


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

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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