• Start

Languages

/

.NET

/

Concepts

Multiple sessions

The .NET SDK supports multiple isolated sessions within a single connection, each with their own authentication and context.

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.

MethodDescription
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 called, the session cannot be used again.

Returns a list of all active sessions on the current connection.

Method Syntax

await db.Sessions(cancellationToken)
Arguments Description
cancellationToken The cancellationToken enables graceful cancellation of asynchronous operations.
// List all active sessions on the current connection
IEnumerable<Guid> sessions = await db.Sessions();


Creates a new isolated session on the current connection. The new session is independent and maintains its own namespace, database, variables, and authentication state.

Method Syntax

await db.CreateSession(cancellationToken)
Arguments Description
cancellationToken The cancellationToken enables graceful cancellation of asynchronous operations.
// 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" });


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.

Method Syntax

await session.ForkSession(cancellationToken)
Arguments Description
cancellationToken The cancellationToken enables graceful cancellation of asynchronous operations.
// 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);


Closes the current session and disposes of it. After this method is called, the session cannot be used again.

Method Syntax

await session.CloseSession(cancellationToken)
Arguments Description
cancellationToken The cancellationToken enables graceful cancellation of asynchronous operations.
// Close and dispose of the session when done
await session.CloseSession();

// The session can no longer be used after this call

Was this page helpful?