ASP.NET MVC 3,RavenDB,& Autofac Issue Plus 2其他自动提示问题 [英] ASP.NET MVC 3, RavenDB, & Autofac Issue Plus 2 Other Autofac Questions

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

问题描述

注意:这里有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的参数Raven.Client.Document.DocumentSession Session ctor(Raven.Client.Document.DocumentStore,Raven.Client.Document.DocumentSession)'



对我来说这个错误听起来像Autofac不认为它一个DocumentSession然而这就是store.OpenSession()返回,所以应该。任何人都知道会导致这个错误?我没有正确设置会话变量(它与商店变量相同,工作正常)?



可能与上述问题可能相关也可能不相关的另一件事是如何根据请求而不是每个应用程序生命周期向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中执行此操作?

解决方案

'猜测你想要的东西:

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

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



基本上,在网络应用程序中, InstancePerLifetimeScope 处理每个HTTP上下文方面的一个,并且还处理实现 IDisposable


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?

解决方案

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.

这篇关于ASP.NET MVC 3,RavenDB,&amp; Autofac Issue Plus 2其他自动提示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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