问题描述
注:有在这里3个问题,我没有做单独的问题,因为他们都有点与同一code
我有以下的code是注册在Application_Start每个应用程序的生命周期中一次,我RavenDB的连接:
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();
现在这工作得很好,这是应该每应用程序的生命周期内只创建一次。现在,我想在DocumentSession到Autofac增加,所以我想在这个添加在Application_Start:
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();
在我UserRepository我有下面的构造:
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:
无法解析参数的构造函数Raven.Client.Document.DocumentSession会议'太虚.ctor(Raven.Client.Document.DocumentStore,Raven.Client.Document.DocumentSession)
这是错误给我听起来像Autofac不认为它有一个却是DocumentSession什么store.OpenSession()返回所以它应该。任何人都知道什么会导致此错误?我不是正确设置会话变量(这是一样的存储可变的正常工作)?
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)?
其可以或可以不涉及上述问题的另一件事是我怎样每个请求,而不是每应用的生命周期中添加的对象的实例,以Autofac?虽然RavenDB文档库对象只应创建一次是生活中的应用周期,DocumentSession应每一次请求创建(也许它创建每个应用程序级别导致上述错误)。
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).
最后一个问题,我会扔那里大约Autofac(轻度与上述code)是关于释放的对象。如果您在本教程中看看:
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:
<一个href=\"http://$c$cofrob.com/archive/2010/09/29/ravendb-image-gallery-project-iii-the-application-lifecycle.aspx\">http://$c$cofrob.com/archive/2010/09/29/ravendb-image-gallery-project-iii-the-application-lifecycle.aspx
最后一块code的:
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
这code点是prevent泄漏的会话。现在,这是我的东西也是需要担心的Autofac如果是的话,我会怎么做这Autofac?
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?
推荐答案
我猜你想要的东西,如:
I'm guessing you want something like:
builder.Register(c => c.Resolve<DocumentStore>().OpenSession()).InstancePerLifetimeScope();
默认ASP.NET和WCF集成的设置,以便InstancePerLifetimeScope()将附加组件来执行当前Web请求或服务方法调用。 -
基本上,一个Web应用程序, InstancePerLifetimeScope
处理每个HTTP上下文方面之一,也是处置实现的IDisposable $ C任何类型$ C>。
Basically, in a web app, InstancePerLifetimeScope
handles the one per HTTP context aspect, and also disposes any types that implement IDisposable
.
这篇关于ASP.NET MVC 3,RavenDB,&安培; Autofac问题加2其他Autofac问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!