Class

# WebSessionContext

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

The <span class="title-ref">WebSessionContext</span> class allows you to specify which session the `Session</api/web/session>` object should point to for code that does not have access to a session, such server-driven classes (such as sockets or threads) or the `WebApplication</api/web/webapplication>` class.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                 | Type                              | Read-Only | Shared |
|--------------------------------------|-----------------------------------|-----------|--------|
| `Session<websessioncontext.session>` | `WebSession</api/web/websession>` | ✓         |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                          | Parameters                                     | Returns | Shared |
|-----------------------------------------------|------------------------------------------------|---------|--------|
| `Constructor<websessioncontext.constructor0>` | identifier As `String</api/data_types/string>` |         |        |
| `Constructor<websessioncontext.constructor1>` | source As `WebSession</api/web/websession>`    |         |        |
| `Stop<websessioncontext.stop>`                |                                                |         |        |

## Property descriptions

<div id="websessioncontext.session">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebSessionContext.Session

**Session** As `WebSession</api/web/websession>`

> The session the <span class="title-ref">WebSessionContext</span> refers to.
>
> This property is read-only.

## Method descriptions

<div id="websessioncontext.constructor0">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebSessionContext.Constructor

**Constructor**(identifier As `String</api/data_types/string>`)

> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> `Constructors</api/language/constructor>` are special methods called when you create an object with the `New</api/language/new>` keyword and pass in the parameters above.
>
> </div>
>
> Creates a <span class="title-ref">WebSessionContext</span> from the `WebSession.Identifier<websession.identifier>` passed.

<div id="websessioncontext.constructor1">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebSessionContext.Constructor

**Constructor**(source As `WebSession</api/web/websession>`)

> Creates a new <span class="title-ref">WebSessionContext</span> from the `WebSession</api/web/websession>` passed.

<div id="websessioncontext.stop">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

WebSessionContext.Stop

**Stop**

> Immediately stops the session context, rather than waiting until the <span class="title-ref">WebSessionContext</span> has been disposed of.

## Notes

The constructor parameter is required.

Inside user-driven events or threads created inside user-driven events, such as a Button push, the Session object is automatically available. However, in server-driven events, such as a Socket DataAvailable event, there is no way for the framework to know which session to communicate with. Should you need to communicate with a session from one of these events, this class can be used to inform the framework to which session the Session object should point.

The session context automatically stops when the object goes out of scope.

Creating a <span class="title-ref">WebSessionContext</span> within a session will raise an `UnsupportedOperationException</api/exceptions/unsupportedoperationexception>`.

## Sample code

When placed in the `WebApplication.UnhandledException<webapplication.unhandledexception>` event, this example will change the current page to another page from the application (called *ExceptionPage* in this case) to inform each user that the application is about to crash due to an unhandled exception.

``` xojo
' Iterate through all sessions and have them display a new
' ExceptionPage.

For Each activeSession As WebSession In App.Sessions
' Without creating a WebSessionContext here, creating
  ' the page would fail triggering a SessionNotAvailableException.
  Var context As New WebSessionContext(activeSession)

  ' Because we have a context, we can create a new
  ' ExceptionPage and assign it to the session.
  context.Session.CurrentPage = New ExceptionPage
Next
```

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | Web |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `Session</api/web/session>`, `WebSession</api/web/websession>`, `SessionNotAvailableException</api/exceptions/sessionnotavailableexception>` classes.

</div>
