简体   繁体   English

自定义角色提供者,Unity和服务定位器模式

[英]Custom Role Provider, Unity, and Service Locator Pattern

I'm have a similar set up to this question MVC Custom ROles Provider . 我对此问题有类似的设置MVC Custom Roles Provider I have created my custom roles provider. 我已经创建了自定义角色提供程序。 but I would like to Use Unity IOC like the rest of my project. 但是我想像我的项目其余部分一样使用Unity IOC。 I have tried to Implement the Service Locator pattern for this and I am very new to this concept. 我已经尝试为此实现服务定位器模式,并且我对这个概念非常陌生。 I was following this guidance Custom Roles Fine Print and this is where I'm stuck. 我遵循了此指南“ 自定义角色精细打印” ,这就是我遇到的问题。

Here is my WebConfig Role Manager Section 这是我的WebConfig角色管理器部分

<roleManager enabled="true" defaultProvider="CustomRoleProvider">   <providers>
    <clear />
    <add name="CustomRoleProvider" type="Reconciliation.CustomRoleProvider" applicationName="/" />   </providers> 
</roleManager>

I am using a UnityMvcActivator Start Class as follows. 我正在使用UnityMvcActivator起始类,如下所示。 The Start() class is in an external Class library. Start()类在外部类库中。 and it is referenced through from my UI project. 从我的UI项目中引用了它。 That set up my Dependency Injection 设置了我的依赖注入

using CommonServiceLocator;
using System.Linq;
using System.Web.Mvc;
using Unity.AspNet.Mvc;
using Unity.ServiceLocation;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(Bootstrapper.UnityMvcActivator), nameof(Bootstrapper.UnityMvcActivator.Start))]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(Bootstrapper.UnityMvcActivator), nameof(Bootstrapper.UnityMvcActivator.Shutdown))]

namespace Bootstrapper
{
    /// <summary>
    /// Provides the bootstrapping for integrating Unity with ASP.NET MVC.
    /// </summary>
    public static class UnityMvcActivator
    {
        /// <summary>
        /// Integrates Unity when the application starts.
        /// </summary>
        public static void Start() 
        {
            var locator = new UnityServiceLocator(UnityConfig.Container);

            FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
            FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(UnityConfig.Container));

            DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Container));

            ServiceLocator.SetLocatorProvider(() => locator);

            // TODO: Uncomment if you want to use PerRequestLifetimeManager
            //Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));

            bool AlwasyTrue = ServiceLocator.IsLocationProviderSet;
        }

        /// <summary>
        /// Disposes the Unity container when the application is shut down.
        /// </summary>
        public static void Shutdown()
        {
            UnityConfig.Container.Dispose();
        }
    }
}

var AlwasyTrue = ServiceLocator.IsLocationProviderSet; var AlwasyTrue = ServiceLocator.IsLocationProviderSet; Always True here; 这里永远是真实的;

In my Custom Roles Provider Class I have 
using System;
using System.Web.Security;
using Core.Interfaces;
using Unity.ServiceLocation;   <--
    public class CustomRoleProvider : RoleProvider
{

    public override string[] GetRolesForUser(string username)
    {
 bool alwaysFalse = ServiceLocator.IsLocationProviderSet;
    ICustomRoleProviderService roleService =
            ServiceLocator.Current.GetInstance<ICustomRoleProviderService>();
        return roleService.GetRolesForUser(username);
        }
    }

bool alwaysFalse = ServiceLocator.IsLocationProviderSet; bool alwaysFalse = ServiceLocator.IsLocationProviderSet; < Always false here, and is always called after the Unity Start() class. <此处始终为false,始终在Unity Start()类之后调用。

But it throws the error 但这会引发错误 在此处输入图片说明

I'm not entirely sure how to fix it. 我不完全确定如何解决它。 It t says that I need to set my service location provider. 它说我需要设置服务位置提供者。 I believe the issue has something to do with the Unity IOC being in an external class. 我相信这个问题与外部课程中的Unity IOC有关。 It works for DI in other classes but not for Dependency Resolver / service locator in the RolesManager... Stumped.... Any help please. 它适用于其他类中的DI,但不适用于RolesManager中的Dependency Resolver /服务定位器。

solved Turns Out that the issue was in fact exactly what the Error was saying. 解决原来,问题实际上就是错误在说什么。 In the External UnityMvcActivator Class I was using 在我使用的外部UnityMvcActivator类中

using CommonServiceLocator;

and in the CustomRoleProvider class I was using 在我使用的CustomRoleProvider类中

using Unity.ServiceLocation;

I ended up changing out that one Reference and rebuilt and it works as expected. 我最终改变了那个参考书并重新构建,它可以按预期工作。 Took awhile to find this. 花了一段时间找到这个。 Hope this helps someone else if they run into this. 希望这对其他人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM