ASP.NET MembershipProvider 和 StructureMap

发布于 2024-08-31 12:05:20 字数 934 浏览 2 评论 0原文

我在应用程序中使用默认的 AspNetSqlMembershipProvider。身份验证是通过 AuthenticationService 执行的(因为我还支持其他形式的会员资格,例如 OpenID)。

我的 AuthenticationService 将 MembershipProvider 作为构造函数参数,并且我使用 StructureMap 注入依赖项,如下所示:

For<MembershipProvider>().Use(Membership.Provider);

这将使用 web.config 中配置的 MembershipProvider。这一切都非常有效。

但是,现在我已经推出了自己的使用存储库类的 MembershipProvider。由于 MembershipProvider 并不完全是 IoC 友好的,因此我将以下代码添加到 MembershipProvider.Initialize 方法中:

_membershipRepository = ObjectFactory.GetInstance<IMembershipRepository>();

但是,这会引发异常,例如 StructureMap 尚未初始化(无法获取 IMembershipRepository 的实例)。但是,如果我删除代码并在 MembershipProvider 的初始化方法和 StructureMap 引导程序中放置断点,则看起来 StructureMap 在初始化 MembershipProvider 之前已配置。

到目前为止,我唯一的解决方法是将上述代码添加到需要存储库的 MembershipProvider 中的每个方法中。这工作正常,但我很好奇为什么我无法在 Initialize 方法中获取实例。 MembershipProvider 是否执行一些在我自己的应用程序代码之前运行的内部初始化?

谢谢 本

I was using the default AspNetSqlMembershipProvider in my application. Authentication is performed via an AuthenticationService (since I'm also supporting other forms of membership like OpenID).

My AuthenticationService takes a MembershipProvider as a constructor parameter and I am injecting the dependency using StructureMap like so:

For<MembershipProvider>().Use(Membership.Provider);

This will use the MembershipProvider configured in web.config. All this works great.

However, now I have rolled my own MembershipProvider that makes use of a repository class. Since the MembershipProvider isn't exactly IoC friendly, I added the following code to the MembershipProvider.Initialize method:

_membershipRepository = ObjectFactory.GetInstance<IMembershipRepository>();

However, this raises an exception, like StructureMap hasn't been initialized (cannot get instance of IMembershipRepository). However, if I remove the code and put breakpoints at my MembershipProvider's initialize method and my StructureMap bootstrapper, it does appear that StructureMap is configured before the MembershipProvider is initialized.

My only workaround so far is to add the above code to each method in the MembershipProvider that needs the repository. This works fine, but I am curious as to why I can't get my instance in the Initialize method. Is the MembershipProvider performing some internal initialization that runs before any of my own application code does?

Thanks
Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

蓝海 2024-09-07 12:05:20

是的,当 AppDomain 启动时,提供程序由 ASP.Net 运行时初始化,远远早于代码的执行。

您将需要选择另一个点来进行合成,也许是在 Global.Application_??? 中。

Yes, the provider is initialized by the ASP.Net runtime when the AppDomain is spun up, far in advance of any execution of your code.

You will need to choose another point to do your composition, perhaps in Global.Application_???.

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