CastleWindsor LifeStyle.PerWebRequest的行为类似于单例 [英] CastleWindsor LifeStyle.PerWebRequest behaves like singleton
问题描述
我正在尝试创建一个可以注入我的类中的UserService,它将保留当前登录到我的系统的用户。
我正在使用CastleWindsor作为容器。
I am trying to create a UserService that I can inject in my classes, that will hold the user currently logged in to my system. I am using CastleWindsor as my container.
现在,我的问题是我试图将UserService设置为可抛弃式的,以便数据库连接在创建时会吸引用户
Now my problem is that I am trying to make my UserService disposable, so that the databaseconnection fetching the user on creating will also be disposed when the object is destroyed.
我在Global.asax.cs中添加了以下设置:
I added the following setup in my Global.asax.cs:
private static void BootstrapContainer()
{
_container = new WindsorContainer().Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(_container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
GlobalConfiguration.Configuration.DependencyResolver = new WindsorDependencyResolver(_container.Kernel);
_container.Register(Component.For<IUserService>()
.LifestylePerWebRequest()
.ImplementedBy<UserService>());
_container.Register(Component.For<IPrincipal>()
.LifeStyle.PerWebRequest
.UsingFactoryMethod(() => HttpContext.Current.User));
}
在我的 Application_Start $ c中被调用了$ c>。
我的UserService代码如下:
My UserService code is as follows:
public interface IUserService
{
OrganisationBruger User { get; }
int UserId { get; }
}
public class UserService : IUserService, IDisposable
{
private readonly IPrincipal _principal;
private OrganisationBruger _user;
private readonly DatabaseDataContext _db;
public UserService(IPrincipal principal, IDatabaseDataContextFactory dataContextFactory)
{
_principal = principal;
_db = dataContextFactory.GetDataContext();
}
public OrganisationBruger User => _user ?? (_user = GetUser());
public int UserId => Convert.ToInt32(_principal.Identity.Name);
private OrganisationBruger GetUser()
{
return _db.OrganisationBrugers.Single(u => u.ID == UserId);
}
public void Dispose()
{
_db.Dispose();
}
}
在调试代码时,我可以看到我首先触发它,它会正确创建类 UserService.cs
,然后在webrequest之后进行处理。现在我的问题是第二个Web请求似乎不再调用构造函数,因此仅重用了以前创建的对象。这导致 DatabaseContext
已被处置。
When I Debug my code I can see on the very first request I fire it correctly creates the class UserService.cs
and then disposes it after the webrequest. Now my problem is the second web request does not seem to call the constructor anymore thus just reusing the formerly created object. This leads to the DatabaseContext
already being disposed of.
我认为 LifestylePerWebRequest
意味着 UserService
会在每个请求上重新创建。有人可以帮我理解吗?
I thought that LifestylePerWebRequest
meant that the UserService
would get recreated on every request. Can anyone help me understand this?
推荐答案
因此,我首先忽略了模块注册部分。文档。您需要在web.config中添加以下内容:
So first of all I had overlook the "registration of the module"-part in the documentation. You need to add the following to your web.config:
<httpModules>
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor"/>
</httpModules>
第二点我不是百分百确定依赖解析器是如何工作的。问题是使用我的 UserService
作为依赖项的模块之一的生命周期设置为 Singleton
,这是默认行为。在容器中注册模块时,如果没有指定任何生命周期。
Second of all I was not a hundred percent sure how the dependency resolver worked. The problem was that one of the modules using my UserService
as a dependency had its lifecycle set to Singleton
which is default behavior when you specify nothing about the lifecycle when registering your module with the container.
我通过确保每个将UserService用作依赖项的模块都已注册了 LifestylePerWebRequest()生命周期来解决此问题。 code>或
LifestyleTransient()
。
I fixed the problem by making sure that every module that is using my UserService as dependency is also registered with a lifecycle of LifestylePerWebRequest()
or LifestyleTransient()
.
这篇关于CastleWindsor LifeStyle.PerWebRequest的行为类似于单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!