#1 2014-11-03 17:02:09

jonjbar
Member
Registered: 2012-12-27
Posts: 37

@mORMot.ServiceContext.Factory is nil when method is called from Serve

Hi,
I'm trying to access an interfaced based service method. It is working fine when called from the client, but when calling from the server, ServiceContext.Factory is always nil:

  aContext := @mORMot.ServiceContext;
  if Assigned(aContext.Factory) then  //  <---- Factory is nil when called from the server
  ...

I'm using this as I need to access the Rest server from that method, to do a DB.BackupBackground.
How can this be handled ? Should I not rely on ServiceContext and use a global instance of my server instead ? This won't be great as this method is shared by both client and server.

Any help would be greatly appreciated.

Offline

#2 2014-11-03 19:54:55

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,371
Website

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

Yes, there is no factory involved from the server side, by definition.

To access the current TSQLRestServer instance (and e.g. its ORM/CRUD methods), use aContext.Request.Server and not aContext.Factory.Server.
Request.Server is the safe access point to the underlying TSQLRestServer.
See http://synopse.info/fossil/info/66c5abe847

Offline

#3 2014-11-04 10:59:28

jonjbar
Member
Registered: 2012-12-27
Posts: 37

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

aContext.Request is nil too in that case sad

Offline

#4 2014-11-04 14:38:05

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,371
Website

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

So I guess the easiest (and safest/more SOLID) is to inject the TSQLRestServer instance in the interface constructor.
smile

Offline

#5 2014-11-04 15:20:11

jonjbar
Member
Registered: 2012-12-27
Posts: 37

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

OK then, I will do that wink
Thanks!

Offline

#6 2015-06-01 09:47:55

jonjbar
Member
Registered: 2012-12-27
Posts: 37

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

I received the following PM from "douglasmmm" regarding this thread. Unfortunately, I do not recall how this was fixed and do not have access to that test anymore. So I'm posting the message here in case somebody can help him:

douglasmmm wrote:

about you post "@mORMot.ServiceContext.Factory is nil when method is called from Serve" in Synopse forum.

you could implement the solution?
I ask because I could not understand when AB says 'is to inject the TSQLRestServer instance in the interface constructor".

grateful

Offline

#7 2015-06-01 11:36:46

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,371
Website

Re: @mORMot.ServiceContext.Factory is nil when method is called from Serve

Offline

Board footer

Powered by FluxBB