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.
Next: Testing the service >>
More Delphi-Kylix Articles
More By Leidago