Question

I'm attempting to implement Dependency Injection into my architecture (MVC, DDD - Domain Model, Repository). And my architecture includes ASP.NET Identity 2.0.

At this stage, I don't want DI controlling any of the Identity 2.0 objects (UserAdminController, RolesAdminController...). I'd prefer the security objects outside of DI. At this stage, integrating the Identity objects in DI looks very difficult. I had a good look to see if someone has already done this, so I could read and learn how to do this. I couldn't find anything. (Found one post which came close, but no resolution).

Anyway, I've followed the Simple Injector MVC implementation (see standard code below), and trying many things, I believe the problem lies in me calling RegisterMvcControllers.

Correct me if I'm wrong, but this statement will pickup all controllers with their name post-fixed with "controller".

Question: How can I select which controllers get registered with Simple Injector? (Is this called manually registering?)

Any help would be greatly appreciated, as I've spent most of today trying to get my head around all this, and proceed to the next step, i.e. have DI implemented, and instantiating my objects.

...

...

... called from Application_Start()

        // Create a Simple Injector container
        var container = new Container();

        // Configure the container
        InitializeContainer(container);

        container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

        // Verify the container's configuration
        container.Verify();

        DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));


    private static void InitializeContainer(Container container)
    {
        container.Register<MyService1>();            

        container.Register<IMyRepositoryA, MyRepositoryA>();

        // Trying to include Identity into Simple Injector - please ignore
        container.Register<IUserStore<ApplicationUser>>(() => new UserStore<ApplicationUser>(new ApplicationDbContext()));
    }
Was it helpful?

Solution

The RegisterMvcControllers will register the following types:

  • The type must be public
  • The type must implement System.Web.Mvc.IController
  • The type must not be abstract
  • The type must not be a generic type definition
  • Its name must end with "Controller"

You can see what happens here in the source code.

The RegisterMvcControllers extension method calls into the SimpleInjectorMvcExtensions.GetControllerTypesToRegister method to get the list of controllers to register. You can call that method yourself to see what is registered as follows:

var registeredControllerTypes =
    SimpleInjectorMvcExtensions.GetControllerTypesToRegister(
        container, Assembly.GetExecutingAssembly())

So instead of calling RegisterMvcControllers you can register the controllers yourself by calling the GetControllerTypesToRegister method:

var registeredControllerTypes = 
    SimpleInjectorMvcExtensions.GetControllerTypesToRegister(
        container, Assembly.GetExecutingAssembly());

foreach (var controllerType in registeredControllerTypes)
{
    container.Register(controllerType, controllerType, Lifestyle.Transient);
}

This way you can filter out any controller you want to register manually:

var registeredControllerTypes = 
    SimpleInjectorMvcExtensions.GetControllerTypesToRegister(
        container, Assembly.GetExecutingAssembly())
    .Where(type => type.Name != "UserStore`1");

foreach (var controllerType in registeredControllerTypes)
{
    container.Register(controllerType, controllerType, Lifestyle.Transient);
}

Another option is to override the registration:

container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

container.Options.AllowOverridingRegistrations = true;

container.Register<IUserStore<ApplicationUser>>(
    () => new UserStore<ApplicationUser>(new ApplicationDbContext()))

// Always set the option back to false ASAP to prevent configuration errors.
container.Options.AllowOverridingRegistrations = false;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top