When trying to integrate the use of RavenDB into the Service Stack, I came across the following solution proposed for session management:
A: using RavenDB with ServiceStack
The suggestion to use the line below to delete the DocumentSession object after the request was completed was attractive.
container.Register(c => c.Resolve<IDocumentStore>().OpenSession()).ReusedWithin(ReuseScope.Request);
From the fact that I understand the logic of Funq, I am registering a new DocumentSession object with an IoC container that will be allowed for IDocumentSession and will exist only at the time of the request. This seemed like a very clean approach.
However, since then I have come across the following absence of exceptions for Max RavenDB sessions:
The maximum number of requests (30) allowed for this session has been reached. Raven limits the number of remote calls that can be made as an early warning system. Sessions are expected to be short, and Raven provides features such as Load (string []) to load multiple documents at once and save packages.
Now, if I am missing something, I should not press the request cap for one session if each session exists only for one request. To get around this problem, I tried the following, pretty bad solution to no avail:
var session = container.Resolve<IDocumentStore>().OpenSession();
session.Advanced.MaxNumberOfRequestsPerSession = 50000;
container.Register(p => session).ReusedWithin(ReuseScope.Request);
Here is an example of how I use a permitted instance of DocumentSession:
private readonly IDocumentSession _session;
public UsersService(IDocumentSession session)
{
_session = session;
}
public ServiceResponse<UserProfile> Get(GetUser request)
{
var response = new ServiceResponse<UserProfile> {Successful = true};
try
{
var user = _session.Load<UserProfile>(request.UserId);
if (user == null || user.Deleted || !user.IsActive || !user.IsActive)
{
throw HttpError.NotFound("User {0} was not found.".Fmt(request.UserId));
}
response.Data = user;
}
catch (Exception ex)
{
_logger.Error(ex.Message, ex);
response.StackTrace = ex.StackTrace;
response.Errors.Add(ex.Message);
response.Successful = false;
}
return response;
}
, SS + RavenDB " ", , , , . , , .