Skip to main content

RVN011 - Use batch.OpenSession inside a subscription Run delegate

Description​

A subscription worker passes each batch of documents to the Run delegate as a SubscriptionBatch<T>.


Sessions opened from the batch:
A session opened from the batch (batch.OpenSession() / batch.OpenAsyncSession()) is bound to that batch:

  • It uses the subscription's database and sends its requests to the node the subscription worker is connected to.
    store.OpenSession() opens a session on the store's default database, which is not necessarily the subscription's database.

  • The batch items are already tracked by the session. Loading an item by its ID does not send a request to the server, and changes made to item.Result are saved when you call SaveChanges().

  • Documents, counters, and time series included by the subscription query are already loaded into the session,
    so accessing them does not send a request to the server.


The problem with opening a session from the store:
A session opened directly from the store is not automatically preloaded with the batch items or included data.
Loading those documents may require additional server requests, and changes made directly to item.Result are not automatically tracked by that session.


Tracking and preloading exceptions:
Nothing is preloaded into a batch session opened with NoTracking = true.
If the subscription returns projections or revisions, the items are not tracked; included data is still preloaded.

Trigger condition​

When is RVN011 reported?​

RVN011 is reported when OpenSession or OpenAsyncSession is called on an IDocumentStore (or a type that implements it, such as DocumentStore) inside a lambda passed directly to the Run method of a subscription worker (SubscriptionWorker<T>).

This includes calls in a nested lambda, anonymous method, or non-static local function inside the Run lambda.
All overloads are reported, including OpenSession(SessionOptions) and OpenSession(string database).

Reported on the method name.

When is RVN011 not reported?​

  • A batch handler passed as a method group, such as worker.Run(ProcessBatch).
  • A lambda stored in a variable and then passed to Run, such as worker.Run(handler).
  • An anonymous method passed to Run,
    such as worker.Run(delegate (SubscriptionBatch<Order> batch) { … }).
  • Calls inside a static local function within the Run lambda, which cannot access the batch parameter.
  • Calls outside a subscription worker's Run lambda.

How to resolve​

Use a batch session:
Open the session from the batch: replace store.OpenSession() / store.OpenAsyncSession() with batch.OpenSession() / batch.OpenAsyncSession(), where batch is the parameter of the Run lambda.

Batch session requirements:

  • A session can be opened from the batch only once per batch; a second call throws InvalidOperationException.
    If the Run delegate opens more than one session, for example a session per item inside a loop, use a single session for the whole batch.

  • The batch session always uses the subscription's database; the batch has no OpenSession(string database) overload. When passing SessionOptions, do not set Database, RequestExecutor, or a TransactionMode other than SingleNode; setting any of these throws InvalidOperationException.

  • Pass a new SessionOptions instance for each batch. Opening a batch session sets its Database and RequestExecutor properties, so reusing the instance for another batch throws InvalidOperationException.

When to keep the store session:
If an operation intentionally requires another database or a cluster-wide transaction,
keep the store session and suppress RVN011 for that call.
Learn how to suppress RVN011 in Suppress diagnostics.


Before (triggers RVN011)​

worker.Run(async batch =>
{
// Triggers RVN011: the session is opened from the store.
using var session = store.OpenAsyncSession();
foreach (var item in batch.Items)
{
// Sends a request to the server for each item.
var order = await session.LoadAsync<Order>(item.Id);
// ... process order
}
await session.SaveChangesAsync();
});

After (correct)​

worker.Run(async batch =>
{
using var session = batch.OpenAsyncSession();
foreach (var item in batch.Items)
{
// Served from the session; no request to the server.
var order = await session.LoadAsync<Order>(item.Id);
// ... process order
}
await session.SaveChangesAsync();
});

Automated code fix​

How does the code fix rewrite your code?​

The code fix replaces the store in the reported call with the Run lambda's batch parameter, keeping the method name and arguments. For example, store.OpenSession(options) becomes batch.OpenSession(options),
and store.OpenAsyncSession(options) becomes batch.OpenAsyncSession(options).

The code fix uses the actual parameter name, even when it is not named batch.

Applied to the Before example, it produces code equivalent to the After example.

When is the code fix not offered?​

RVN011 can report a call even when the automated code fix is unavailable.
In the following cases, change the code manually:

  • The call is inside a nested lambda or anonymous method within the Run lambda,
    which may run after the batch is processed.
  • The call is inside a loop within the Run lambda.
  • The Run lambda contains more than one call named OpenSession or OpenAsyncSession,
    including calls that already open a session from the batch.
  • The call uses the OpenSession(string database) or OpenAsyncSession(string database) overload,
    which the batch does not have.
  • SessionOptions is passed as a named argument, such as sessionOptions: … or options: ….
  • SessionOptions is constructed inline or in a local variable initializer that explicitly assigns Database, RequestExecutor, or TransactionMode.
    This check also rejects explicitly setting TransactionMode to SingleNode.

What should you check manually?​

The code fix does not verify all batch session constraints or all effects of tracking batch items.
Before applying the code fix, make sure that:

  • The Run delegate opens only one session per batch, including calls made through helper methods or repeated calls to a local function.
  • The SessionOptions passed do not set Database, RequestExecutor, or a TransactionMode other than SingleNode. The initializer check does not cover values assigned later or options supplied through a helper method, parameter, field, or property.
  • A new SessionOptions instance is passed for each batch; an instance used to open a previous batch session already has Database and RequestExecutor set.
  • Changes made directly to tracked item.Result objects are intended to be saved when SaveChanges() / SaveChangesAsync() is called.
  • The code does not store a different object instance under an ID already tracked by the batch session. For example, session.Store(newObject, item.Id) throws NonUniqueObjectException while item.Result is tracked under item.Id.

Limitations​

  • Null-conditional calls are not reported, such as store?.OpenSession() or worker?.Run(…).

In this article