Registering DataSnap Server Class in runtime with Delphi

Categories:DelphiTags: , ,
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 Responses to Registering DataSnap Server Class in runtime with Delphi

  1. Reply Tweets that mention Registering DataSnap Server Class in runtime with Delphi | Andreano Lanusse English Blog -- Topsy.com says:

    […] 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 […]

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

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

  4. Reply 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”).

Leave a Reply

Specify Facebook App ID in Super Socializer > Social Login section in admin panel for Facebook Login to work

Specify Twitter Consumer Key and Secret in Super Socializer > Social Login section in admin panel for Twitter Login to work

Specify LinkedIn API Key in Super Socializer > Social Login section in admin panel for LinkedIn Login to work

Specify GooglePlus Client ID in Super Socializer > Social Login section in admin panel for GooglePlus Login to work

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