自定义角色提供者、Unity 和服务定位器模式
Custom Role Provider, Unity, and Service Locator Pattern
我对这个问题有类似的设置 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. 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,这就是我遇到的问题。
这是我的 WebConfig 角色管理器部分
<roleManager enabled="true" defaultProvider="CustomRoleProvider"> <providers>
<clear />
<add name="CustomRoleProvider" type="Reconciliation.CustomRoleProvider" applicationName="/" /> </providers>
</roleManager>
我正在使用UnityMvcActivator Start Class如下。 Start() class 位于外部 Class 库中。它是从我的 UI 项目中引用的。这设置了我的依赖注入
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;这里永远是真的;
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; < 此处始终为 false,并且始终在 Unity Start() 之后调用 class。
但是它抛出错误
我不太确定如何修复它。它说我需要设置我的服务位置提供商。我认为这个问题与 Unity IOC 在外部 class 中有关。它适用于其他 classes 中的 DI,但不适用于 RolesManager 中的依赖项解析器/服务定位器......难倒......请提供任何帮助。
已解决事实证明问题实际上正是错误所说的。在外部 UnityMvcActivator Class 我使用的是
using CommonServiceLocator;
我在 CustomRoleProvider class 中使用
using Unity.ServiceLocation;
我最终更改了那个参考并重新构建,它按预期工作。花了一段时间才找到这个。希望这对 运行 感兴趣的其他人有所帮助。
我对这个问题有类似的设置 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. 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,这就是我遇到的问题。
这是我的 WebConfig 角色管理器部分
<roleManager enabled="true" defaultProvider="CustomRoleProvider"> <providers>
<clear />
<add name="CustomRoleProvider" type="Reconciliation.CustomRoleProvider" applicationName="/" /> </providers>
</roleManager>
我正在使用UnityMvcActivator Start Class如下。 Start() class 位于外部 Class 库中。它是从我的 UI 项目中引用的。这设置了我的依赖注入
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;这里永远是真的;
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; < 此处始终为 false,并且始终在 Unity Start() 之后调用 class。
但是它抛出错误
我不太确定如何修复它。它说我需要设置我的服务位置提供商。我认为这个问题与 Unity IOC 在外部 class 中有关。它适用于其他 classes 中的 DI,但不适用于 RolesManager 中的依赖项解析器/服务定位器......难倒......请提供任何帮助。
已解决事实证明问题实际上正是错误所说的。在外部 UnityMvcActivator Class 我使用的是
using CommonServiceLocator;
我在 CustomRoleProvider class 中使用
using Unity.ServiceLocation;
我最终更改了那个参考并重新构建,它按预期工作。花了一段时间才找到这个。希望这对 运行 感兴趣的其他人有所帮助。