The .NET SDK allows you to create multiple isolated sessions within a single connection. Each session maintains its own namespace, database, variables, and authentication state, while sharing the underlying connection to SurrealDB. This is useful when different parts of your application need to operate under different credentials or contexts simultaneously.
Method | Description |
|---|---|
db.Sessions() | List all active sessions on the current connection. |
db.CreateSession() | Create a new isolated session on the current connection. |
session.ForkSession() | Creates a copy of a session, inheriting its state. |
session.CloseSession() | Closes the current session and disposes of it. After this method is |
.Sessions()
Returns a list of all active sessions on the current connection.
await db.Sessions(cancellationToken)Arguments
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous |
Example usage
// List all active sessions on the current connection
IEnumerable<Guid> sessions = await db.Sessions(); .CreateSession()
Creates a new isolated session on the current connection. The new session is independent and maintains its own namespace, database, variables, and authentication state.
await db.CreateSession(cancellationToken)Arguments
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous |
Example usage
// Create a new isolated session on the same connection
var session = await db.CreateSession();
// Use the session independently
await session.Use("my_namespace", "my_database");
await session.SignIn(new RootAuth { Username = "root",
Password = "root" }); .ForkSession()
Creates a copy of the current session, inheriting its namespace, database, variables, and authentication state. Changes made in the forked session do not affect the original.
await session.ForkSession(cancellationToken)Arguments
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous |
Example usage
// Fork the current session to create an independent copy
var forkedSession = await session.ForkSession();
// The forked session inherits namespace, database, and auth state
// Changes made here do not affect the original session
await forkedSession.Set("x", 42); .CloseSession()
Closes the current session and disposes of it. After this method is called, the session cannot be used again.
CloseSession() only closes the session itself. The underlying connection shared with the parent SurrealDbClient remains open.
await session.CloseSession(cancellationToken)Arguments
Arguments | Description |
|---|---|
cancellationToken | The cancellationToken enables graceful cancellation of asynchronous |
Example usage
// Close and dispose of the session when done
await session.CloseSession();
// The session can no longer be used after this call