在配置中定义的ASP MVC 5.0 RoleProvider团结依赖注入 [英] Unity dependency injection in an ASP MVC 5.0 RoleProvider defined in configuration

查看:99
本文介绍了在配置中定义的ASP MVC 5.0 RoleProvider团结依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现使用一个单独的EF库类角色提供,作为注射的依赖,进入我的角色商店。我的问题是,角色提供在配置(web.config中)中所定义,因此,不通过统一DI容器实例。我一直没能找到一种方法,要么转向的配置,code或它的建成调用container.BuildUP()上后,即可获取角色提供的搁置。任何建议将是AP preciated。

I'm trying to implement a role provider using a separate EF repository class, as an injectable dependency, to access my roles store. My problem is that the role provider is defined in configuration (web.config) and therefore is not instantiated via the Unity DI container. I haven't been able to find a way to either shift the configuration to code or get hold of the role provider after it's built to call container.BuildUP() on it. Any suggestions would be appreciated.

推荐答案

我觉得我的问题的解决方案是相当不错这里介绍:

I think that my question's solution is fairly well covered here:

<一个href=\"http://stackoverflow.com/questions/10174636/property-injection-in-custom-membership-provider-using-castle\">Property注射自定义的成员提供使用城堡

如果我已经搜查的MembershipProvider,而不是RoleProvider很可能会通过发现它的第一次。

If I'd searched on MembershipProvider instead of RoleProvider probably would have found it the first time through.

就总结一下我的解决方案,链接带领我到在$公共服务定位​​器库 C $ CPLEX。

Just to summarize my solution, the link lead me to the Common Service Locator library at codeplex.

它并为它统一适配器包含在的NuGet包统一3.所以,我已经受够了。

It and the Unity adapter for it are included in the Nuget package for Unity 3. So I already had it.

我添加一行到我CompositionRoot类的撰写()方法的末尾:

I added one line to the end of the Compose() method of my CompositionRoot class:

ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));

和我现在可以访问定位器/容器直通静态的ServiceLocator类我RoleProvider的构造:

And I can now access the locator/container thru the static ServiceLocator class in the constructor of my RoleProvider:

    public IAuthorizationManager Manager {get; set;}

    public MyRoleProvider()
    {
        var locator = ServiceLocator.Current;
        Manager = locator.GetInstance<IAuthorizationManager>();
    }

(当然,你需要'使用'语句Microsoft.P​​ractices.ServiceLocation)

(Of course, you need 'using' statements for Microsoft.Practices.ServiceLocation)

这篇关于在配置中定义的ASP MVC 5.0 RoleProvider团结依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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