ASP.NET MVC 3、RavenDB 和Autofac 问题加上 2 其他 Autofac 问题

发布于 2024-10-29 17:45:15 字数 1638 浏览 3 评论 0原文

注意:这里有 3 个问题,我没有提出单独的问题,因为它们都与相同的代码有些相关。

我有以下代码,每次在 Application_Start 中注册一次与我的 RavenDB 的连接应用程序的生命周期:

var store = new DocumentStore { Url = "http://localhost:8080" };
store.Initialize();

builder.RegisterInstance(store).SingleInstance();

现在这工作正常,并且每个应用程序的生命周期只应创建一次。现在我想将 DocumentSession 添加到 Autofac,因此我尝试在 Application_Start 中添加此内容:

var session = store.OpenSession();
builder.RegisterInstance(session).SingleInstance();

在我的 UserRepository 中,我有以下构造函数:

 public UserRepository(DocumentStore store, DocumentSession session)

当我尝试运行它时,出现以下运行时错误:

无法解析参数构造函数 'Void .ctor(Raven.Client.Document.DocumentStore, Raven.Client.Document.DocumentSession)' 的 'Raven.Client.Document.DocumentSession Session'

对我来说,这个错误听起来像 Autofac 认为它没有但是,这就是 store.OpenSession() 返回的 DocumentSession,所以它应该返回。有谁知道什么会导致这个错误?我是否没有正确设置会话变量(它与工作正常的商店变量相同)?

与上述问题可能相关或无关的另一件事是如何根据请求而不是根据应用程序生命周期将对象实例添加到 Autofac?虽然 RavenDB DocumentStore 对象只应在应用程序生命周期中创建一次,但 DocumentSession 应根据请求创建一次(也许每个应用程序级别创建它会导致上述错误)。

我要提出的关于 Autofac 的最后一个问题(与上面的代码略有相关)是关于释放对象的。如果您看一下本教程:

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

最后一段代码:

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

以及要点这段代码的目的是为了防止泄露会话。现在这是我也需要为 Autofac 担心的事情吗?如果是这样,我将如何在 Autofac 中做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流殇 2024-11-05 17:45:31

还有一个问题是 OpenSession 返回 IDocumentSession 而不是 DocumentSession。更改我的课程以寻找 IDocumentSession 并按照 Jim 的建议进行操作,谢谢。

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.

翻了热茶 2024-11-05 17:45:30

我猜您想要类似的内容:

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

“设置默认的 ASP.NET 和 WCF 集成,以便 InstancePerLifetimeScope() 将组件附加到当前的 Web 请求或服务方法调用。” - Autofac: InstanceScope

基本上,在网络应用中,InstancePerLifetimeScope 处理每个 HTTP 上下文方面,并且还处理实现 IDisposable 的任何类型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文