We have successfully activated sessions using the following configuration:
cfg.ReceiveEndpoint(endpoint.Item1, e =>
{
e.RequiresSession = true;
e.SessionIdleTimeout = TimeSpan.FromMinutes(10);
e.ConfigureConsumer(context, endpoint.Item2);
});
Our use-case is to keep the session open for as long as a database cursor is open (currently 10 minutes in Cosmos). We use the session to ensure that a subsequent fetch from the cursor is routed to the specific consumer that has the open cursor.
What we'd like to do is to close the session when we have reached the last record in the cursor. Additionally, we'd like to use the same endpoint for standard CRUD operations that don't really need the session at all. For these activities, I'd like to close the session immediately.
The MS docs say:
The session lock should be treated like an exclusive lock on a file, meaning that the application should close the session as soon as it no longer needs it and/or doesn't expect any further messages.
And it seems we might be able to call something like:
_session.CloseAsync();
But I can't seem to find a way to do that through MassTransit.
The session is only accessible while a message is being consumed, and I don't believe that you can close the session while the message is being consumed. If you've built a proof-of-concept of how this might be done, you can share it or submit a pull request to MassTransit to add it.