Wisej.Web.Application.Session is null in another thread.



When i access Wisej.Web.Application.Session from another thread, it is always null. Please advise the solution.


Here is my code:

Public Sub Main()

Wisej.Web.Application.Session.SessionVar = “ABC”


Dim window As New Window1()

End Sub

Async Sub ReadSessionVarAsync()
Dim sessionVar As String = String.Empty
Await Threading.Tasks.Task.Run(Sub()

‘Wisej.Web.Application.Session always nothing in this thread.
sessionVar = Wisej.Web.Application.Session(“SessionVar”)

End Sub)

End Sub

  • Tung Ngo
    Please note that Sub ReadSessionVarAsync is a simplified example for illustration only. In reality, this sub is in a third party dll, which I have no access to it. So there is no change for me to use Wisej.Web.Application.StartTask. Besides, Async Sub ReadSessionVarAsync() is used widely in Business Object layer, which has no knowledge about User Interface layer. (Wisej/Winform/Mobile)
  • Luca (ITG)
    I read this comment I posted the StartTask suggestion. The only way to restore the session is to have a reference to the session. The session is referenced in all Wisej components or in a cookie or in the URL – otherwise there is no way for a thread to know about a specific session in any web system. You can also use Application.RestoreSession(HttpContext), in which case you don’t need a Wisej component but you need the http context to read the session id from the cookie or the URL.
  • You must to post comments

Hi Tung,

The session is always null in a new thread unless Wisej has a chance to restore it. See this thread: https://wisej.com/support/question/updating-ui-elements-from-other-threads

You can restore the session on any thread in several ways:

1- Use Application.StartTask(()=>{ …code in context…});

2-Use Application.RunInContext(this, ()=>{… code in context..}); You need “this” to be a component (control, page, window, etc.)

The best way is no. 1. See the BackgroundTasks example in the examples section and see the background tasks help section: https://docs.wisej.com/docs/concepts/background-tasks.



  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.