Skip to content

Instantly share code, notes, and snippets.

@zbozkurt
Created October 17, 2022 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbozkurt/413bccd7d70f283e5f69c15ff71b7d98 to your computer and use it in GitHub Desktop.
Save zbozkurt/413bccd7d70f283e5f69c15ff71b7d98 to your computer and use it in GitHub Desktop.
Mormot interface based service client error
Changed server section to working multiple TSQLRestServer with TServiceFactoryServer
like this
var
xxModel: Array Of TSQLModel;
xxRestServer : Array Of TSQLRestServer;
xxHttpServer : TSQLHttpServer;
xxServiceFactoryServer : Array Of TServiceFactoryServer;
xcounter : Integer;
...
xcounter:=0;
xServicePort:='9031';
xxService_RootName:='bank';
SetLength(xxModel,xcounter+1);
SetLength(xxRestServer,xcounter+1);
SetLength(xxServiceFactoryServer,xcounter+1);
xxModel[xcounter]:=TSQLModel.Create([], xxService_RootName);
xxRestServer[xcounter]:= TSQLRestServerFullMemory.Create(xxModel[xcounter], false);
xxServiceFactoryServer[xcounter] := xxRestServer[xcounter].ServiceDefine(TBankMethods, [IBankMethods], sicSingle);
xxServiceFactoryServer[xcounter].SetOptions([], [optErrorOnMissingParam, optForceStandardJSON]);
inc(xcounter);
xServicePort:='9031';
xxService_RootName:='customer'; // daha sonra ayarlanacak....
SetLength(xxModel,xcounter+1);
SetLength(xxRestServer,xcounter+1);
SetLength(xxServiceFactoryServer,xcounter+1);
xxModel[xcounter]:=TSQLModel.Create([], xxService_RootName);
xxRestServer[xcounter]:= TSQLRestServerFullMemory.Create(xxModel[xcounter], false);
xxServiceFactoryServer[xcounter] := xxRestServer[xcounter].ServiceDefine(TCustomerMethods, [ICustomerMethods], sicSingle);
xxServiceFactoryServer[xcounter].SetOptions([], [optErrorOnMissingParam, optForceStandardJSON]);
..
xxHttpServer := TSQLHttpServer.Create(AnsiString(xServicePort), xxRestServer, '+', useHttpSocket);
xxHttpServer .AccessControlAllowOrigin:='*';
THttpServer(xxHttpServer .HttpServer).ServerKeepAliveTimeOut := 30000;
I didn't see any error on server side. But I have got sometimes on client side.
Client side look likes
type
lRootName=(Customer, Bank, Stock) ;
tRestClient = class
private
fModel: TSQLModel;
fClientSettings: rClientSettings;
fConnectionSettings: rConnectionSettings;
fInitialized: boolean;
public
fClient: TSQLRestClientURI;
CustomerMethods : ICustomerMethods; // Customer
BankMethods: IBankMethods; // Bank
StockMethods : IStockMethods; // Stock
...
function Initialize(ClSettings: rClientSettings; ConSettings: rConnectionSettings; XROOTNAME:LRootName): boolean; overload;
function Initialize(ClSettings: rClientSettings; XROOTNAME:LRootName): boolean; overload;
end;
...
function tRestClient.Initialize(ClSettings: rClientSettings; XROOTNAME:LRootName): boolean;
var
xRootName: String;
begin
Result := False;
// Destroy current object
IF fInitialized then
DeInitialize();
// Client initialization (for better understanding, each section contain separate code, later should be refactored)
fClientSettings := ClSettings;
// lRootName=(Customer, Bank, Stock) ;
case lRootName of
Customer : xRootName:='Customer';
Bank : xRootName:='Bank';
Stock : xRootName:='Stock';
end;
fModel := TSQLModel.Create([], xRootName);
case fClientSettings.Protocol of
//
HTTP_Socket:
begin
fClient := TSQLHttpClientWinSock.Create(AnsiString(fClientSettings.HostOrIP),
AnsiString(fClientSettings.Port), fModel, false, '', '',
fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWinSock(fClient).KeepAliveMS := XXCONNECTION_TIMEOUT;
end;
//
HTTP_WebSocket:
begin
fClient := TSQLHttpClientWebsockets.Create(AnsiString(fClientSettings.HostOrIP),
AnsiString(fClientSettings.Port), fModel, false, '', '',
fConnectionSettings.SendTimeout, fConnectionSettings.ReceiveTimeout,
fConnectionSettings.ConnectTimeout);
TSQLHttpClientWebsockets(fClient).KeepAliveMS := XXCONNECTION_TIMEOUT;
end;
...
end;
case fClientSettings.AuthMode of
// NoAuthentication
NoAuthentication:
begin
// nothing to do here
end;
...
end;
// Service initialization
TRY
case lRootName of
Customer : BEGIN
// xRootName:='Customer';
fClient.ServiceDefine([ICustomerMethods], SERVICE_INSTANCE_IMPLEMENTATION); // SERVICE_INSTANCE_IMPLEMENTATION =SciSingle
Result := fClient.Services.Resolve(ICustomerMethods, CustomerMethods); // Sometimes error occur Access violation
END;
Bank : BEGIN
// xRootName:='Bank';
fClient.ServiceDefine([IBankMethods], SERVICE_INSTANCE_IMPLEMENTATION); // SERVICE_INSTANCE_IMPLEMENTATION =SciSingle
Result := fClient.Services.Resolve(IBankMethods, BankMethods); // Sometimes error occur Access violation
END;
Stock : BEGIN
// xRootName:='Stock';
fClient.ServiceDefine([IStockMethods], SERVICE_INSTANCE_IMPLEMENTATION); // SERVICE_INSTANCE_IMPLEMENTATION =SciSingle
Result := fClient.Services.Resolve(IStockMethods, StockMethods); // Sometimes error occur Access violation
END;
end;
EXCEPT
Result:=false;
END;
..
fInitialized:=Result;
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment