Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

NOTE: There are 3 questions in here and I did not make separate questions since they are all somewhat related to the same code.

I have the following code that registers the connection to my RavenDB in the Application_Start once per the application's 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 be created only once per the application's life cycle. Now I wanted to add in the DocumentSession to Autofac so I tried to add in this in the 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 follow runtime error:

Cannot resolve parameter 'Raven.Client.Document.DocumentSession Session' of constructor 'Void .ctor(Raven.Client.Document.DocumentStore, Raven.Client.Document.DocumentSession)'

That error to me sounds like Autofac does not think it has a DocumentSession however that is what store.OpenSession() returns so it should. Anyone know what would be causing this error? Am I not setting the session variable correctly (it is the same as the store variable which works fine)?

Another thing which may or may not be related to the above issue is how do I add an instance of an object to Autofac per request instead of per the applications life cycle? While the RavenDB DocumentStore object should only be created once be the life application cycle, the DocumentSession should be created once per the request (maybe creating it per application level is causing the error above).

One last question I will throw there about Autofac (mildly related to the code above) is about releasing the objects. If you take a look at this tutorial:

http://codeofrob.com/archive/2010/09/29/ravendb-image-gallery-project-iii-the-application-lifecycle.aspx

The last piece of code:

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

and the point of this code is to prevent leaking the sessions. Now is this something I also need to worry about for Autofac and if so, how would I do this in Autofac?

like image 763
ryanzec Avatar asked Apr 01 '11 17:04

ryanzec


2 Answers

I'm guessing you want something like:

builder.Register(c => c.Resolve<DocumentStore>().OpenSession()).InstancePerLifetimeScope();

"The default ASP.NET and WCF integrations are set up so that InstancePerLifetimeScope() will attach a component to the current web request or service method call." - Autofac: InstanceScope

Basically, in a web app, InstancePerLifetimeScope handles the one per HTTP context aspect, and also disposes any types that implement IDisposable.

like image 145
Jim Bolla Avatar answered Oct 31 '22 05:10

Jim Bolla


There was also the issue that OpenSession returns a IDocumentSession instead of a DocumentSession. Changing my class to look for a IDocumentSession along with doing what Jim suggested worked, thanks.

like image 42
ryanzec Avatar answered Oct 31 '22 05:10

ryanzec