Custom Role Provider using DI ninject throwing error

前端 未结 3 513
悲哀的现实
悲哀的现实 2021-01-19 06:09

I setup a CustomRoleProver as:

public class CustomRoleProvider : RoleProvider
    {
        private readonly IRepository _repository;

        pu         


        
3条回答
  •  耶瑟儿~
    2021-01-19 07:08

    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.

提交回复
热议问题