I'm have a similar set up to this question 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 and this is where I'm stuck.
Here is my WebConfig Role Manager Section
<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. The Start() class is in an external Class library. and it is referenced through from my UI project. 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; 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; < Always false here, and is always called after the Unity Start() class.
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. It works for DI in other classes but not for Dependency Resolver / service locator in the RolesManager... Stumped.... Any help please.
solved Turns Out that the issue was in fact exactly what the Error was saying. In the External UnityMvcActivator Class I was using
using CommonServiceLocator;
and in the CustomRoleProvider class I was using
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.