DelphI Labs: DataSnap XE - Windows Service DataSnap Server

By: Pawel Glowacki

Abstract: In this Delphi Labs episode we are looking at different DataSnap application types and as a demo project Delphi DataSnap server application is implemented as Windows Service.

    Introduction

DataSnap architecture is very flexible and open. Currently you can use Delphi and C++Builder to implement DataSnap servers and you can implement clients using Delphi, C++, Delphi Prism, PHP and JavaScript.

DataSnap servers can be standalone or implemented as web application. You can also use different protocols like TCP/IP or HTTP for communication between clients and servers.

You can find in-depth discussion of different DataSnap application types, communication protocols and options in Jim Tierney’s “DataSnap Application Types” CodeRage5 conference presentation. The recording is available at http://channel-e.embarcadero.com/index.php?option=com_jvideodirect&x=1&v=Qxne3uhuP1k1T.

Trying to build all possible types of servers and clients and possible communication would be a very interesting, but rather a time consuming activity.

In this tutorial I’m going to discuss steps needed to implement DataSnap server using Delphi as “Windows Service”. This is not a very frequently used feature in DataSnap demos; however implementing DataSnap servers as operating system services has a lot of sense. Basically when your server is deployed as an OS service into a machine, then it starts automatically on start-up and you do not have to interact with a newly created machine instance in your – for example Amazon EC2 – cloud. You just need to start a machine or even better have your load balancer automatically start it for you!

Let’s give it a try.

    Building the Server as Windows Service

Click on the “File – New – Other” menu and inside the “New Items” dialog double-click on the “DataSnap Server” wizard in “Delphi Projects – DataSnap Server” category to create a standalone Delphi DataSnap server application.

Hide image
DataSnapServerWiz

In the first tab of the wizard select “Windows Service” as the application type.

Hide image
Click to see full-sized image

expand view>>

Keep the default values for server features, so our service is going to use TCP/IP protocol for communication, no authorization/authentication and generate sample server methods.

Hide image
Click to see full-sized image

expand view>>

On the third screen keep the default TCP/IP port 211 and check if it is open.

Hide image
Click to see full-sized image

expand view>>

On the last page of the wizard keep the default base class of the server methods implementation and click on “Finish”.

Select “File – Save All” to save all the files in the new project generated by the wizard. In my case I am going to create a new “C:\DataSnapLabs\WinServiceApp\” folder for my files. Keep the default names of the server methods and server container units (for example “ServerMethodsUnit2” and “ServerContainerUnit2”) and name the whole project as “DataSnapWinServiceApp”.

The server methods unit is exactly the same like for other Delphi DataSnap server application types. The server container unit and the main project unit contain some Delphi code that is specific to “Windows Service” application type.

My DataSnap server service application looks like this in the Project Manager:

Hide image
DataSnapServerServicePrjInProjMgr

Open the server container unit and have a look at the code generated by the wizard.

unit ServerContainerUnit2;

interface

uses
  SysUtils, Classes, 
  SvcMgr, 
  DSTCPServerTransport,
  DSServer, DSCommonServer, DSAuth; 

type
  TServerContainer2 = class(TService)
    DSServer1: TDSServer;
    DSTCPServerTransport1: TDSTCPServerTransport;
    DSServerClass1: TDSServerClass;
    procedure DSServerClass1GetClass(DSServerClass: TDSServerClass;
      var PersistentClass: TPersistentClass);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
  private
    { Private declarations }
  protected
    function DoStop: Boolean; override;
    function DoPause: Boolean; override;
    function DoContinue: Boolean; override;
    procedure DoInterrogate; override;
  public
    function GetServiceController: TServiceController; override;
  end;

var
  ServerContainer2: TServerContainer2;

implementation

uses Windows, ServerMethodsUnit2;

{$R *.dfm}

procedure TServerContainer2.DSServerClass1GetClass(
  DSServerClass: TDSServerClass; var PersistentClass: TPersistentClass);
begin
  PersistentClass := ServerMethodsUnit2.TServerMethods2;
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  ServerContainer2.Controller(CtrlCode);
end;

function TServerContainer2.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

function TServerContainer2.DoContinue: Boolean;
begin
  Result := inherited;
  DSServer1.Start;
end;

procedure TServerContainer2.DoInterrogate;
begin
  inherited;
end;

function TServerContainer2.DoPause: Boolean;
begin
  DSServer1.Stop;
  Result := inherited;
end;

function TServerContainer2.DoStop: Boolean;
begin
  DSServer1.Stop;
  Result := inherited;
end;

procedure TServerContainer2.ServiceStart(Sender: TService; var Started: Boolean);
begin
  DSServer1.Start;
end;

end.

The first thing to notice is the fact that the server container class is derived from the “TService” class defined in the “SvcMgr” unit and it overrides some of the virtual methods from this class, namely: “DoStop”, “DoPause”, “DoContinue” and “DoInterrogate”. These are the methods that are called in response to operation inside the “Services” applet that is part of Windows.

Now we need to build our project. You can do it a number ways. For example by right-clicking in the Project Manager and selecting “Build”.

Hide image
BuildProj

Now is the time to install our service in Windows, so it appears in the “Services” applet.

In the default build configuration you should find your executable in the “Debug\Win32” subfolder of the directory where your project resides. In my case this is “C:\DataSnapLabs\WinServiceApp\Debug\Win32\DataSnapWinServiceApp.exe”.

Open Windows command prompt as administrator and navigate to this directory.

Hide image
Click to see full-sized image

In order to install the service you need to start the executable from the command line passing “/INSTALL” parameter. To uninstall the service just pass “/UNINSTALL”.

expand view>>

Make sure to start the console as administrator, otherwise you will not be able to install the service.

The Windows Service application do not have any user interface and the only way to interact with it is through operating system “Services” applet.

In order to verify that our service application is running we need to check it in services, or just try to call its functionality from any client.

If you open “Task Manager” you should be able that our service “ServerContainer2” is also running. For this make sure to check the option to display services from users.

Hide image
Click to see full-sized image

If you use Windows utility “Resource Manager” you should see the following:

expand view>>

Hide image
Click to see full-sized image

We could have been a basic client app, but it is probably faster to use “Data Explorer” and check if we can test connect to the server and view its server methods.

expand view>>

Create a new connection to a DataSnap server. Just right-click on the “DATASNAP” node in the “Data Explorer” and select “Add Connection”.

You can optionally rename the connection, for example to “TCP_211_LOCAL”.

Select “Modify Connection” and click on “Test Connection” button to see if the server is running.

Hide image
Click to see full-sized image

It looks like our Delphi DataSnap Server implemented as Windows Service has been successfully deployed!

expand view>>

The key benefit of implementing a DataSnap server as an OS service is the fact that you do not have to worry about starting your services. By default they are started automatically.

    Summary

The DataSnap architecture is like a glue to connect different types of application in different programming languages available in RAD Studio. You can currently implement servers in Delphi and C++Builder and clients in Delphi, C++Builder, RadPHP, Delphi Prism and JavaScript.

In this “Delphi Labs” episode we have looked into the steps of building Delphi DataSnap server application as a “Windows Service” application. OS services do not have any user interface, so the only way to interact with them is via Service Control Manager. Applications implemented as services by default start automatically on OS start-up, so it is very useful for managing server instances deployed to EC2 Cloud, where load balancer starts and stops instances automatically on behalf of the administrator.

The in-depth discussion of different DataSnap application types can be found on the Channel-E: “DataSnap Application Types” CodeRage5 conference presentation at http://channel-e.embarcadero.com/index.php?option=com_jvideodirect&x=1&v=Qxne3uhuP1k1T.

The source code for this article is available at the Code Central ((http://cc.embarcadero.com/item/28270) and the video version of this demo is available on YouTube (http://www.youtube.com/watch?v=ovZ-W4qVWgQ).

More information about "Delphi Labs":

Server Response from: ETNASC03