#1 Re: mORMot 1 » ServiceContext.Request become nil cause error » 2015-12-11 11:50:51

Thanks for proper points and IoC. But Need some pratice to understand TInterfacedObjectWithCustomCreate or TInterfaceObjectAutoCreateFields and TInjectableObject tongue

Indeed fClient used as a proxy to fetch data from lServer, to feed TRemote@rServer to the rClient. I m confused why fClient clear the rServer's serviceContext?

#2 mORMot 1 » ServiceContext.Request become nil cause error » 2015-12-11 09:28:13

Wideyu
Replies: 3

@ab

i m learning how to control mORMot, not sure any wrong with my code:

program Project1;

{$APPTYPE CONSOLE}

uses
  mORMot, mORMotSQLite3, SynSQLite3Static, System.SysUtils;

type
  IRemote = interface(IInvokable)
    ['{48FCB3D5-0992-48A4-ACFD-13FE652802FB}']
    function TestOK: TID;
    function TestError: TID;
  end;
  TRemote = class(TInterfacedObject, IRemote)
  private
    fClient: TSQLRestClientDB;
  public
    procedure AfterConstruction; override;
    function TestOK: TID;
    function TestError: TID;
  end;

var
  rServer, lServer: TSQLRestServerDB;
  rClient: TSQLRestClientDB;
  fRemote: IRemote;

procedure TRemote.AfterConstruction;
begin
  inherited;
  fClient := TSQLRestClientDB.Create(lServer);
end;

function TRemote.TestOK: TID;
var
  aContext: TSQLRestServerURIContext;
begin
  aContext := ServiceContext.Request;
  fClient.ServerTimeStampSynchronize;
  ServiceContext.Request := aContext;
  result := ServiceContext.Request.SessionUser;
end;

function TRemote.TestError: TID;
begin
  fClient.ServerTimeStampSynchronize;
  //ServiceContext.Request=nil here
  result := ServiceContext.Request.SessionUser;
end;

begin
  lServer := TSQLRestServerDB.Create(TSQLModel.Create([]), 'local.db3', false);
  lServer.CreateMissingTables();

  rServer := TSQLRestServerDB.Create(TSQLModel.Create([]), 'remote.db3', true);
  rServer.CreateMissingTables();
  rServer.ServiceRegister(TRemote,[TypeInfo(IRemote)],sicClientDriven);

  rClient := TSQLRestClientDB.Create(rServer);
  rClient.SetUser('User', 'synopse');
  rClient.ServiceRegisterClientDriven(TypeInfo(IRemote), fRemote);

  Writeln('TestOK result: ');
  Writeln(fRemote.TestOK);
  
  try
    Writeln('TestError result: ');
    Writeln(fRemote.TestError);
  except
    on E: Exception do
      writeln(E.Message);
  end;
  readln;
end.

console output

TestOK result:
3

TestError result:
TInterfacedObjectFakeClient.FakeCall(IRemote.TestError) failed: '{
"errorCode":500,
"error":
{"EAccessViolation":{"EAccessViolation":"Access violation at address 00660ADF in
 module 'Project1.exe'. Read of address 00000120"}}
}'

Board footer

Powered by FluxBB