Custom variables in context

I would need to carry some custom informations retrieved when crreating server / modules (from ini files or database) so they are available for each request (ie in TXDataOperationContext.Current)


Is there a way to do so ?


 

You should create your own data structure. One thing you can do is use a similar approach as described here:


https://www.tmssoftware.com/site/tips.asp?s=faq&show=954

In summary, you create a thread variable:


threadvar  
  MyContext: TMyContext;


Then add a Generic middleware to your XData server, and add the following code in the OnRequest event:



      procedure TDataModule1.MiddlewareRequest(Sender: TObject;Context: THttpServerContext; Next: THttpServerProc);
      begin  
        MyContext := TMyContext.Create;
        try
          Next(C);  
        finally
          MyContext.Free;
        end;
      end;

 

Maybe I'm getting it wrong but with your suggestion TMyContext would be only available in middleware scope.


My need is :

At server creation:

Read conf 1
Read conf 2
....

When processing requests:

TMyServiceImplementation.MyMethod1
begin
if <condition1> then <use conf1>
if <condition2> then <use conf2>
....
end;

TMyServiceImplementation.MyMethod2
begin
if <condition1> then <use conf1>
if <condition2> then <use conf2>
....
end;

I had thought about the header transporting the needed informations to compute condition1 ...
What I don't  get is how I can access conf1, conf2 .... within request context without using a thread safe global structure.

Just declare the MyContext threadvar in a common unit used by both the middleware and your module code. That is not a problem.

But in this case the config (conf1,conf2...) would be read for each request which I dont want. I need the config to be read once when server starts

 
Then just create and load conf1 and conf2, put them in a global variable, and simply read them.
If you don't modify conf1 and conf2, they will likely be thread safe.