如何控制 MembershipProvider 实例的创建/生命周期?

发布于 2024-10-02 12:31:59 字数 2033 浏览 6 评论 0原文

我已在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-10-09 12:31:59

我刚刚在博客中介绍了这个问题并提供了一个解决方案。

简而言之,该解决方案涉及一个简单的、可重用的 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.

夏有森光若流苏 2024-10-09 12:31:59

不用担心您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文