ASP.NET MVC 3、RavenDB 和Autofac Issue Plus 2 其他 Autofac 问题 [英] ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

查看:36
本文介绍了ASP.NET MVC 3、RavenDB 和Autofac Issue Plus 2 其他 Autofac 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有以下代码在每个应用程序的生命周期中在 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:

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

这个错误对我来说听起来像 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 DocumentStore 对象应该只在应用程序生命周期中创建一次,但是 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 的问题(与上面的代码稍微相关)是关于释放对象.如果您看一下本教程:

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

最后一段代码:

ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();

这段代码的目的是防止会话泄漏.现在,这是我也需要为 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 请求或服务方法调用."- Autofac:InstanceScope

"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

基本上,在 Web 应用程序中,InstancePerLifetimeScope 处理每个 HTTP 上下文方面的一个,并且还处理任何实现 IDisposable 的类型.

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 Issue Plus 2 其他 Autofac 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆