Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
class function TIWExceptionRendererEx.RenderHTML
#1
Hi,

I'm using this function with great success.
But I need to reference a variable in the UserSession. How do I do that?
Reply
#2
I suppose that within your code you have access to your WebApplication instance, is that correct?

The property Data of the WebApplication contains a reference to your UserSession object:

UserSession := TIWUserSession(WebApplication.Data);

However, the typecast will require you to link the unit with this code to the UserSessionUnit where TIWUserSession is declared.

Another alternative is to use the UserSessionBase (the parent class of your UserSession) Clipboard object to temporarily store some values that are needed elsewhere. Something like:

uses
IWUserSessionBase;

UserSession := TIWUserSessionBase(WebApplication.Data);

// Store the value there
UserSession.Put("LoggedUser", "Mikael Nilsson");

// Store the value there
UserName := UserSession.Get("LoggedUser");

Note that in this example I don't need to link with the UserSessionUnit, only with the IWUserSessionBase unit which is part of the IW not your application. You can have a more generic approach using this.
Reply
#3
(04-07-2023, 12:17 AM)Alexandre Machado Wrote: I suppose that within your code you have access to your WebApplication instance, is that correct?

The property Data of the WebApplication contains a reference to your UserSession object:

UserSession := TIWUserSession(WebApplication.Data);

However, the typecast will require you to link the unit with this code to the UserSessionUnit where TIWUserSession is declared.

Another alternative is to use the UserSessionBase (the parent class of your UserSession) Clipboard object to temporarily store some values that are needed elsewhere. Something like:

uses
  IWUserSessionBase;

UserSession := TIWUserSessionBase(WebApplication.Data); 

// Store the value there
UserSession.Put("LoggedUser", "Mikael Nilsson"); 

// Store the value there
UserName := UserSession.Get("LoggedUser");

Note that in this example I don't need to link with the UserSessionUnit, only with the IWUserSessionBase unit which is part of the IW not your application. You can have a more generic approach using this.

Hi,

Sorry I don't understand thcode.  It's hard for me to understand what procedure/function you have assigned that code.

What I understand  TIWExceptionRendererEx.RenderHTML(AException: Exception;  ARequest: THttpRequest): string; executes first and then 
TIWServerController.IWServerControllerBaseCloseSession(aSession: TIWApplication);

I want to send a value from TIWExceptionRendererEx.RenderHTML to a variable in the userssion.
and then IWServerControllerBaseCloseSession use that variable so i can update my table with the exception class.

--------------------------------------------------------------------------------------------------------------------------------------------------
Reply
#4
So use the first line that I wrote in my response:

UserSession := TIWUserSession(WebApplication.Data);

Now you have your UserSession and is able to call it's methods
Reply
#5
(04-07-2023, 09:36 AM)Alexandre Machado Wrote: So use the first line that I wrote in my response:

UserSession := TIWUserSession(WebApplication.Data);

Now you have your UserSession and is able to call it's methods

If I put that row first in TIWExceptionRendererEx.RenderHTML I get a compile error: Lefts side cannot be assign to
Reply
#6
You need to declare a variable of type TIWUserSession to assign the value to.

In the example above I used UserSession but probably the compiler is trying to use the UserSession function. Use a different name instead:

as I mentioned before, I'm assuming you have access to the WebApplication. If not, use gGetWebApplicationThreadVar() instead. The complete code should be:

Code:
uses
  UserSessionUnit, IWApplication;

procedure DoSomethingWithUserSession();
var
  WebApp: TIWApplication;
  theUserSession: TIWUserSession;
begin
  WebApp := gGetWebApplicationThreadVar();
  theUserSession  := nil;
  if Assigned(WebApp) then  // there is no guarantee that a TIWApplication instance exists when an exception is raised
    theUserSession := TIWUserSession(WebApp.Data);
  if Assigned(theUserSession) then
  begin
    // do what you have to do with the UserSession here
  end;
end;
Reply
#7
(04-07-2023, 08:50 PM)Alexandre Machado Wrote: You need to declare a variable of type TIWUserSession to assign the value to.

In the example above I used UserSession but probably the compiler is trying to use the UserSession function. Use a different name instead:

as I mentioned before, I'm assuming you have access to the WebApplication. If not, use gGetWebApplicationThreadVar() instead. The complete code should be:

Code:
uses
  UserSessionUnit, IWApplication;

procedure DoSomethingWithUserSession();
var
  WebApp: TIWApplication;
  theUserSession: TIWUserSession;
begin
  WebApp := gGetWebApplicationThreadVar();
  theUserSession  := nil;
  if Assigned(WebApp) then  // there is no guarantee that a TIWApplication instance exists when an exception is raised
    theUserSession := TIWUserSession(WebApp.Data);
  if Assigned(theUserSession) then
  begin
    // do what you have to do with the UserSession here
  end;
end;

Hi,

Unfortunately it seems that the TIWApplication instance don't exists when an exception is raised.
So I guess that is it. Or is it another solution?

I hope you understand I'm doing this code in the ServerController?
Reply
#8
The exception handler executes in case any exception is raised, including the ones that occur outside the scope of a session. In this case, no IWApplication instance will exist. In some other cases it will...
That's why our TIWExceptionLogger was designed to save a plain text file on the hard disk, because the disk is always available.
Reply
#9
(04-07-2023, 10:35 PM)Alexandre Machado Wrote: The exception handler executes in case any exception is raised, including the ones that occur outside the scope of a session. In this case, no IWApplication instance will exist. In some other cases it will...
That's why our TIWExceptionLogger was designed to save a plain text file on the hard disk, because the disk is always available.

Hi,

So is it possible to read that log file from IWServerControllerBaseCloseSession?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)