注入的MembershipProvider到ASP.Net MVC的AccountController [英] Inject a MembershipProvider into ASP.Net MVC AccountController

本文介绍了注入的MembershipProvider到ASP.Net MVC的AccountController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.Net MVC 1.0项目模板包括的AccountController 类,它支持构造器注入:

Asp.Net MVC 1.0 project templates include an AccountController class, which supports constructor injection:

public AccountController(IFormsAuthentication formsAuth, 
    IMembershipService service)
{
    FormsAuth = formsAuth ?? new FormsAuthenticationService();
    MembershipService = service ?? new AccountMembershipService();
}

AccountMembershipService 还包括类,它也支持构造器注入:

An AccountMembershipService class is also included, and it too supports constructor injection:

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider ?? Membership.Provider;
}

我敢肯定,你们中许多人已经使用了这些单元测试,但我的目标是要注入的的SqlMembershipProvider 使用温莎,因而使用温莎XML文件,而不是web.config文件在运行时配置。换句话说,我确实希望使用构造函数注入的AccountMembershipService类,然后我想继续使用内置在ASP.Net 2.0会员制度。我只想会员系统的配置要经过国际奥委会温莎

I'm sure many of you have used these for unit testing, but my goal is to inject a SqlMembershipProvider using Windsor, and thus configure it at runtime using Windsor xml files rather than web.config. In other words, I want to actually use constructor injection for the AccountMembershipService class, and I want to continue using the built in ASP.Net 2.0 Membership system. I just want the configuration of the Membership system to go through Windsor IoC.

这是可能没有写我自己的MembershipProvider,还是不SqlMembershipProvider的与IOC发挥好?

Is this possible without writing my own MembershipProvider, or does SqlMembershipProvider not play well with IoC?

从MSDN :。该SqlMembershipProvider的构造是由ASP.NET称为在应用程序的配置中指定创建的SqlMembershipProvider类的实例,此构造方法的不打算从code使用的。

From MSDN: "The SqlMembershipProvider constructor is called by ASP.NET to create an instance of the SqlMembershipProvider class as specified in the configuration for the application. This constructor is not intended to be used from your code."

我相信菲尔问了一个很类似的问题,但我没有足够的经验来神交<一个href=\"http://haacked.com/archive/2007/11/27/question-for-you-dependency-injection-buffs.aspx#feedback\"相对=nofollow> 他收到的答案。

I believe Phil asked a very similar question, but I'm not experienced enough to grok the answers he received.

感谢您的帮助。



更新:
只是要清楚,我之所以suppling通过DI应用程序的MembershipProvider是支持多个租户。每个租户有一个孤立的数据库ASP会员表的。 DI让我在运行时切换连接字符串,从而避免了核心应用无关约被用于为每个租户的数据库。温莎控制DI和它知道该租户通过URL发出请求:

Thanks for your help.


UPDATE: Just to be clear, my reason for suppling the application's MembershipProvider via DI is to support multiple tenants. Each tenant has an isolated database with ASP membership tables. DI allows me to switch connection strings at runtime and thus keep the core application agnostic about which database is being used for each tenant. Windsor is controlling the DI and it "knows" which tenant makes the request via url:

var url = HttpContext.Current.Request.ServerVariables["HTTP_HOST"]

迈克哈德洛这里这种技术会谈。 我只是想SqlMembershipProvider的融入他使用这种IoC的设计。

Mike Hadlow talks about this technique here. I am just trying to integrate SqlMembershipProvider into his use of this IoC design.

再次感谢。

推荐答案

假设你有你的会员资格供应商配置是这样的:

Assuming you have your membership providers configured something like this:

<membership>
    <providers>
    	<clear/>
    	<add name="www.tenant1.com" 
         type="System.Web.Security.SqlMembershipProvider, ..." 
         .../>
    	<add name="www.tenant2.com" 
         type="System.Web.Security.SqlMembershipProvider, ..." 
         .../>
    </providers>
</membership>

您可以拥有温莎选择合适的供应商是这样的:

you can have Windsor select the appropriate provider like this:

var container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<MembershipProvider>()
    .LifeStyle.Transient
    .UsingFactoryMethod(() => Membership.Providers[HttpContext.Current.Request.Url.Host]));
... (your controller registrations, etc)

这篇关于注入的MembershipProvider到ASP.Net MVC的AccountController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆