Registering DataSnap Server Class in runtime with Delphi
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.
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.
@Hugo my source was correct, the server will register the classes during the startup process, not after
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.
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”).