Registering DataSnap Server Class in runtime with Delphi

Let's share the knowledge with your friends

When we are working to create a DataSnap application we need to register the Server classes in order to provide access to the client application to the server methods. The natural way is to use the DSServerClass component, but some time we want do it in run time. The following code shows how to do that, the TSimpleServerClass inherit from TDSServerClass and overload/reintroduce the Create method, adding the parameters ServerClass, DataSnap Server and LifeCycle.

type

  TSimpleServerClass = class(TDSServerClass)
  private
    FPersistentClass: TPersistentClass;
  protected
    function GetDSClass: TDSClass; override;
  public
    constructor Create(AOwner: TComponent; AServer: TDSCustomServer; AClass: TPersistentClass; ALifeCycle: String); reintroduce; overload;
  end;

procedure RegisterServerClasses(AOwner: TComponent; AServer: TDSServer);

implementation

constructor TSimpleServerClass.Create(AOwner: TComponent; AServer: TDSCustomServer; AClass: TPersistentClass; ALifeCycle: String);
begin
  inherited Create(AOwner);
  FPersistentClass := AClass;
  Self.Server := AServer;
  Self.LifeCycle := ALifeCycle;
end;

function TSimpleServerClass.GetDSClass: TDSClass;
begin
  Result := TDSClass.Create(FPersistentClass, False);
end;

Now we just need to instantiate the TSimpleServerClass for each server class.

The following code register 3 classes on the same server using different LifeCycles.

procedure RegisterServerClasses(AOwner: TComponent; AServer: TDSServer);
begin
  Assert(AServer.Started = false, 'Can''t add class to non active Server');

  TSimpleServerClass.Create(AOwner, AServer, TGlobal, TDSLifeCycle.Server);
  TSimpleServerClass.Create(AOwner, AServer, TCustomer, TDSLifeCycle.Session);
  TSimpleServerClass.Create(AOwner, AServer, TObjectPool, TDSLifeCycle.Invocation);
end;

Simple and useful.


Let's share the knowledge with your friends
6 replies
  1. Hugo Carrillo
    Hugo Carrillo says:

    I try Delphi XE and used code explain in this article, but, the i change the sentence

    Assert(AServer.Started = false, ‘Can”t add class to non active Server’);

    by

    Assert(AServer.Started = true, ‘Can”t add class to active Server’);

    When the server is active, not show class and methods to client app. Only work when the server is not active.

    Reply
  2. Hugo Carrillo
    Hugo Carrillo says:

    Andreano, thanks for your answer, but i insist. I put DSServer with AutoStart to False (in design). In onCreate of DataModule i assign the port of DSTCPServerTransport and start DSServer

    DSTCPServerTransport.Port := getPort();
    DSServer.Start;
    TSimpleServerClass.Create(Self, DSServer, TMYClassServer, TDSLifeCycle.Session);

    And i get “Method not found in the server method list” for all calls. When i put code in diferent order

    DSTCPServerTransport.Port := getPort();
    TSimpleServerClass.Create(Self, DSServer, TMYClassServer, TDSLifeCycle.Session);
    DSServer.Start;

    work fine for me.

    Andreano, I have some servers in DataSnap 2010 and i saw some behaviors not expected, for example:
    – Memory used by server.
    – When method return DBXReader or other objects type, i can’t get parameter by name,
    – And others

    But, in this moment i try Delphi XE and i see that those behaviors are no longer more. now I see behaviors that have pleasantly surprised me.
    – Better performance in memory management
    – I get parameters by name for DBXReader parameters.
    – Higher speed. Datasnap in XE is very fast respect Datasnap 2010.

    Reply
  3. Joel
    Joel says:

    Regarding Hugo’s comment, your code was correct but your assert error message is wrong (which is why he was confused). The error message should say ‘can”t add class to active server’ (not “non active server”).

    Reply

Trackbacks & Pingbacks

  1. […] This post was mentioned on Twitter by Embarcadero Tech, Embarcadero Asia. Embarcadero Asia said: Registering DataSnap Server Class in runtime with Delphi http://bit.ly/eUAB0U […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.