4

I setup a CustomRoleProver as:

public class CustomRoleProvider : RoleProvider
    {
        private readonly IRepository<User> _repository;

        public CustomRoleProvider(IRepository<User> repository)
        {
            _repository = repository;
        }

...

In my Global.asax.cs I have:

//Create Ninject DI kernel
            var kernel = new StandardKernel();

            kernel.Bind<IRepository<User>>().To<Repository<User>>();

            //Tell ASP.NET MVC 3 to use our Ninject DI Container
            DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

When I try to run my code I get:

Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.

Parser Error Message: No parameterless constructor defined for this object.

Source Error:

Line 64: Line 65: Line 66: Line 67: Line 68:

So maybe I have a totally incorrect understanding of DI and ninject. I thought that when it tried to instantiate teh CustomRoleProvider it would create the user repository and pass it to the constructor not trying to use a parameterless one.

Do I understand this incorrectly or is my setup just incorrect?

I find this strange because the DI seems to be mostly working for the controllers.

AnonyMouse
  • 18,108
  • 26
  • 79
  • 131

3 Answers3

4

You're writing a roleprovider, but these are not currently DI aware. A lot of the classes in MVC are DI aware, which is why you see it workign with controllers.

To be able to inject a constructor parameter the DI container has to be responsible for creating the object you're hoping to inject into. Just registering a type does not magically make it locatable.

With roleproviders, it is the Roles class within the framework that create instances of the providers, including your custom provider. It only works parameterless constructors. You'll need to use something like property injection within your custom provider and use Roles.Provider to get the instance to inject your dependencies into.

Simon Halsey
  • 5,459
  • 1
  • 21
  • 32
0

In order to setup Ninject personally I would create a factory class within your project as per below.

Note, the AddBindings method where you configure the DI bindings.

public class NinjectControllerFactory : DefaultControllerFactory
{
    private IKernel ninjectKernel;
    public NinjectControllerFactory()
    {
        ninjectKernel = new StandardKernel();
        AddBindings();
    }
    protected override IController GetControllerInstance(RequestContext requestContext,
    Type controllerType)
    {
        return controllerType == null
        ? null
        : (IController)ninjectKernel.Get(controllerType);
    }
    private void AddBindings()
    {
        // put bindings here
        ninjectKernel.Bind<IRepository<User>>().To<Repository<User>>();
    }
}

Register the factory within Application_Start of Global.asax.cs:

ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
Jesse
  • 8,223
  • 6
  • 49
  • 81
0

Most likely you are using an invalid custom dependency resolver. Read https://github.com/ninject/ninject.web.mvc/wiki/MVC3 about how to use the Ninject.MVC3 extension instead.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98