To the main page...The list of my products...Some texts...Sample applications, tips, tricks...If you need support...
 

 
Self-starting configurable service
Step 1. Re-using service module

Likely you've seen multi-service applications that allow to install two or more services using the same executable file and the same service implementation. Such functionality can be useful for services that work with databases (one base - one service) or any other configurable resource.

Is it possible to reuse the service module in Delphi? If you mean the TService then the answer is NO! Unbelievable? Yes, but it is so. The reason is simple - TService descendants have the Controller method that refers the global variable in the same unit. You can create as many modules as you need - but you can't create more global variables. You will have to edit the code each time you will need to add one more service to your application.

Fortunately SvCom has no this problem so many instances of the service module can be created and used. All necessary steps are explained below.

Well, the idea of this example is to create the service application that installs and runs several instances of the same service module. The first thing we should care about is the service names. The service module can be reused. The service name can not. And the service display name can not be reused too - each service in the system must have the unique name and display name.

Let's consider names and display names as configuration parameters. There are many ways and many places to store configuration parameters - and this example uses simple ini file for it. Create the SvComEx22.ini file and insert the following lines to it:


 

[Options]
Count=3

Name1=Ex22SvcA
DisplayName1=SvCom Example 22 - Service "A"

Name2=Ex22SvcB
DisplayName2=SvCom Example 22 - Service "B"

Name3=Ex22SvcC
DisplayName3=SvCom Example 22 - Service "C"

I hope you see how this file will be used: it will be three services and their names will be Ex22SvcA, Ex22SvcB and Ex22SvcC as well as display names will be SvCom Example 22 - Service "A", SvCom Example 22 - Service "B" and SvCom Example 22 - Service "B" respectively.

Well, create new NTService Application now and add one TNTService to it. Store the project as SvComEx22 and modify the project source as shown below (new lines marked by color):




















 
 

 

 

 

 

 
 


 


 
 


 

 
 



 
 

 

 
 

program SvComEx22;

uses
    WinSvc,
    SvCom_WinSvc,
    SvCom_NTService,
    Windows,
    IniFiles,
    Registry,
    SysUtils,
    svcExample22 in 'svcExample22.pas' {svcServiceBase: TNtService};

{$R *.RES}


procedure CreateNewServices;
var Ini: TIniFile;
    i, Count: Integer;
    Svc: TNTService;
begin
    Ini:=TIniFile.Create(ChangeFileExt(ParamStr(0), '.INI'));

    Count:=Ini.ReadInteger('Options','Count',1);

    for i:=1 to Count do
    begin
        Svc:=TsvcServiceBase.Create(Application);

        Svc.Name:=Ini.ReadString('Options','Name'+IntToStr(i),'');

        if Svc.Name = '' then
            raise Exception.CreateFmt('Service name '+
                      'missed for the service %d',[i]);

        Svc.DisplayName:=Ini.ReadString('Options',
                                 'DisplayName'+IntToStr(i),'');

        if Svc.DisplayName = '' then
            raise Exception.CreateFmt('Service display name'+
                ' missed for the service %d',[i]);

    end;

    Ini.Free;
end;


var ServiceName: String;
begin
    Application.Initialize;

    CreateNewServices;

    Application.Run;
end.

Note that usual Application.CreateForm(...) line replaced by CreateNewServices call. Look on it. As you see it is quite simple: it (a) opens ini file and (b) reads the number of services and (c) creates service module instances and sets their names and display names.

Try to compile and run this example in the debug mode. You will see that each service has its own page and can be started and stopped independently.

Debug mode screenshow

To install services use the /install switch. Check our services in the control panel all three services are there. Try to start them, stop them - everything works! Finally uninstall them with /uninstall switch.

Ok, as you see everything is fine. There is only one problem: it is not safe to change the services configuration. Suppose that someone have changed the ini file say removed one service entry from it. Now this service is damaged, it can be neither uninstalled nor started/stopped.

The next step suggests a solution of this problem. The idea is simple again: it is to store the service configuration into registry and uses ini file at installation phase only.

The source code of this step is available here (zip, 2.5kb).

<< | Index | Step 1 | Step 2 | Step 3 | Step 4 | Step 5 | Step 6 | >>
Add your comment | Read comments


 
© 1998-2001 Alexey Dynnikov
My ICQ # is 18267212