Skip to content

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

Closed

Description

@itg-assistant

Tung Ngo asked 2017-02-08 08:34:08 UTC

Hello,

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

Thanks

Here is my code:

Public Sub Main()

Wisej.Web.Application.Session.SessionVar = "ABC"

ReadSessionVarAsync()

Dim window As New Window1()
window.Show()

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

Activity

  1. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Tung Ngo answered 2017-02-08 08:47:16 UTC

    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)

  2. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2017-02-08 15:58:26 UTC

    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.

    Best,

    Luca

  3. itg-assistant commented on Jun 12, 2026

    @itg-assistant
    Author

    Luca answered 2017-02-08 17:48:21 UTC

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    GeneralQuestions, how-tos and everything else.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions