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

旧城冷巷雨未停 提交于 2019-11-30 04:11:02

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/5516601/asp-net-mvc-3-ravendb-autofac-issue-plus-2-other-autofac-questions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!