NOTE. There are three questions here, and I did not ask individual questions, since they are all related to the same code.
I have the following code that registers a connection with my RavenDB in Application_Start once per application life cycle:
var store = new DocumentStore { Url = "http://localhost:8080" }; store.Initialize(); builder.RegisterInstance(store).SingleInstance();
Now this works fine, and this is something that should only be created once per application life cycle. Now I wanted to add to the DocumentSession for Autofac, so I tried to add this to Application_Start:
var session = store.OpenSession(); builder.RegisterInstance(session).SingleInstance();
In my UserRepository, I have the following constructor:
public UserRepository(DocumentStore store, DocumentSession session)
When I try to run this, I get the following runtime error:
Cannot resolve the parameter "Raven.Client.Document.DocumentSession Session" of the constructor "Void.ctor (Raven.Client.Document.DocumentStore, Raven.Client.Document.DocumentSession)"
This error sounds to me like Autofac, doesn't think it has a DocumentSession, but that is what store.OpenSession () is returning, so it should. Does anyone know what might cause this error? Am I not setting the session variable correctly (is it the same as the storage variable that works fine)?
Another thing that may or may not be related to the above problem is how to add an object instance to Autofac for each request, and not to the application life cycle? Although the RavenDB DocumentStore object should only be created after the life cycle, the DocumentSession must be created once for the request (perhaps creating it at the application level raises the error above).
One final question that I will talk about Autofac (regarding the related code) is how to free objects. If you look at this tutorial:
http://codeofrob.com/archive/2010/09/29/ravendb-image-gallery-project-iii-the-application-lifecycle.aspx
Last piece of code:
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
and the point of this code is to prevent session leakage. Now I also need to worry about Autofac, and if so, how do I do this in Autofac?