我在web.config文件中注册了一个自定义的membershipprovider类。我正在使用Castle Windsor使用反向控制,并且我已经将自定义的MembershipProvider类注册为Transient(因为它使用的服务也是Transient)。
这意味着我希望在每个Web请求上重新创建成员资格提供程序实例。目前,每个应用程序域只创建一次,因此当它试图访问它所依赖的服务时,该服务实例将被重用,而不应该被重用。
现在我需要找到一种方法让温莎控制我的自定义会员资格提供商的生存期,但我不知道怎么做。我期望在.NET框架中的某个地方有一个工厂,允许我重写实例创建并将其重新路由到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);
}
}