如何控制 MembershipProvider 实例的创建/生命周期?
我已在 Web.Config 文件中注册了一个自定义 MembershipProvider 类。我正在使用 Castle Windsor 进行控制反转,并且我已将自定义 MembershipProvider 类注册为瞬态(因为它也使用瞬态服务)。
这意味着我希望在每个 Web 请求上重新创建会员提供程序实例。目前,每个应用程序域仅创建一次它,因此当它尝试访问它所依赖的服务时,该服务实例会在不应该的情况下被重用。
现在我需要找到一种方法让 Windsor 控制我的自定义 MembershipProvider 的生命周期,但我不知道如何做。我期望在 .NET Framework 中的某个位置有一个工厂,允许我覆盖实例创建并将其重新路由到 Windsor,但我找不到类似的东西。
顺便说一句,我正在使用.NET 4.0。
更新:这是我的一些代码,以便您可以准确地看到我在做什么:
Web.Config:
<membership defaultProvider="MyMembershipProvider" >
<providers>
<clear/>
<add name="ApplicationMembershipProvider"
type="MyNamespace.MyMembershipProvider, MyAssembly"/>
</providers>
</membership>
会员提供商
public class MyMembershipProvider : MembershipProvider
{
private IMyService myService;
public MyMembershipProvider() : base()
{
// We should use constructor injection here but since we cannot control
// the construction of this class, we're forced to create the dependency
// ourselves.
}
public override bool ValidateUser(string username, string password)
{
if (myService == null)
{
// This scope is only reached once within the browser session,
// ASP.NET keeps the instance of MyMembershipProvider in memory
// so the myService field keeps its value across web requests.
// This results in Castle Windsor (which I have configured the service
// locator to use) not being able to control the lifetime of
// the MyService instance. So, the inability of Windsor to control
// the lifetime of MembershipProvider instances, inhibits the lifetime
// management of MyService instances as well.
myService = ServiceLocator.Current.GetInstance<IMyService>();
}
return myService.ValidateUser(username, password);
}
}
I have registered a custom MembershipProvider class in my Web.Config file. I'm using Inversion Of Control using Castle Windsor and I have registered my custom MembershipProvider class as transient (because it's using a service that's transient as well).
This means that I want the Membership provider instance recreated on every web request. Currently, it is created only once per application domain so when it tries to access the service it depends on, that service instance is reused while it is not supposed to.
Now I need to find a way of having Windsor control the lifetime of my custom MembershipProvider but I don't know how. I expected a factory sitting around somewhere in the .NET Framework, allowing me to override the instance creation and rerouting it to Windsor but I can't find anything alike.
By the way, I'm using .NET 4.0.
UPDATE: Here's some of my code so you can see what I'm doing exactly:
Web.Config:
<membership defaultProvider="MyMembershipProvider" >
<providers>
<clear/>
<add name="ApplicationMembershipProvider"
type="MyNamespace.MyMembershipProvider, MyAssembly"/>
</providers>
</membership>
Membership Provider
public class MyMembershipProvider : MembershipProvider
{
private IMyService myService;
public MyMembershipProvider() : base()
{
// We should use constructor injection here but since we cannot control
// the construction of this class, we're forced to create the dependency
// ourselves.
}
public override bool ValidateUser(string username, string password)
{
if (myService == null)
{
// This scope is only reached once within the browser session,
// ASP.NET keeps the instance of MyMembershipProvider in memory
// so the myService field keeps its value across web requests.
// This results in Castle Windsor (which I have configured the service
// locator to use) not being able to control the lifetime of
// the MyService instance. So, the inability of Windsor to control
// the lifetime of MembershipProvider instances, inhibits the lifetime
// management of MyService instances as well.
myService = ServiceLocator.Current.GetInstance<IMyService>();
}
return myService.ValidateUser(username, password);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚在博客中介绍了这个问题并提供了一个解决方案。
简而言之,该解决方案涉及一个简单的、可重用的 MembershipProvider,它调用容器来解析您的自定义 MembershipProvider。与使用“BuildUp”容器功能的其他解决方案不同,此解决方案真正控制实例化,从而实现构造函数注入(进而实现不变性)和代理能力。
I just blogged about this with a solution.
In a nutshell, this solution involves a simple, reusable MembershipProvider that calls the container to resolve your custom MembershipProviders. Unlike other solutions that use "BuildUp" container features, this one takes true control of instantiation, thus enabling constructor injection (which in turn enables immutability) and proxyability.
不用担心您的 MembershipProvider 生命周期。只需管理提供程序内 IMyService 的生命周期即可。使用 getter 为 IMyService 创建一个属性,并在每次请求时返回一个新实例(或者您想要管理生命周期的方式)。
Don't worry about your MembershipProvider lifetime. Simply manage the lifetime of the IMyService within the provider. Create a property for your IMyService with a getter, and return a new instance (or however you want to manage the lifetime) each time it is requested.