4

In trying to integrate RavenDB usage with Service Stack, I ran across the following solution proposed for session management:

A: using RavenDB with ServiceStack

The proposal to use the line below to dispose of the DocumentSession object once the request is complete was an attractive one.

container.Register(c => c.Resolve<IDocumentStore>().OpenSession()).ReusedWithin(ReuseScope.Request);

From what I understand of the Funq logic, I'm registering a new DocumentSession object with the IoC container that will be resolved for IDocumentSession and will only exist for the duration of the request. That seemed like a very clean approach.

However, I have since run into the following max session requests exception from RavenDB:

The maximum number of requests (30) allowed for this session has been reached. Raven limits the number of remote calls that a session is allowed to make as an early warning system. Sessions are expected to be short lived, and Raven provides facilities like Load(string[] keys) to load multiple documents at once and batch saves.

Now, unless I'm missing something, I shouldn't be hitting a request cap on a single session if each session only exists for the duration of a single request. To get around this problem, I tried the following, quite ill-advised solution to no avail:

var session = container.Resolve<IDocumentStore>().OpenSession();
session.Advanced.MaxNumberOfRequestsPerSession = 50000;
container.Register(p => session).ReusedWithin(ReuseScope.Request);

Here is a sample of how I'm using the resolved DocumentSession instance:

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;
}

As far as I can see, I'm implementing SS + RavenDB "by the book" as far as the integration point goes, but I'm still getting this max session request exception and I don't understand how. I also cannot reliably replicate the exception or the conditions under which it is being thrown, which is very unsettling.

Community
  • 1
  • 1
Jason Castellano
  • 333
  • 4
  • 11
  • Looks like you're using raven correctly (except for the "ill-advised" part). I can only speculate that Funq isn't disposing the session. You could try wiring up a different IoC container, as shown [here](https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container). I have had great success with RavenDB and [AutoFac](http://autofac.org/). – Matt Johnson-Pint Feb 04 '14 at 07:13
  • Good suggestion. I had tried implementing AutoFac and wiring up all my dependencies via ContainerBuilder, however I hit a wall when I had to mimic Funq's Container.RegisterValidators in AutoFac. I take it by just creating the AutoFac IoC container adapter, I'm not going to get that RegisterValidators logic for free. – Jason Castellano Feb 04 '14 at 21:36
  • Have you tried running the Raven.Server from the command line and tracing the information from there? Also - there is a profiler for RavenDB to display the queries on the website (http://ravendb.net/docs/2.0/appendixes/profiling-mvc-application) – Dominic Zukiewicz Jul 09 '14 at 10:53

0 Answers0