2

First off I want to say there is a ton of answers on SO and google searches surrounding this, however I'm running into an issue that prevents those solutions from working. The answer here seems to be the way to go. (kernel.Inject(Roles.Provider);)

The issue that I'm having is that when I'm trying to inject the RoleProvider Roles.Provider is null, however my custom provider is found in the list within Roles.Providers. I am thinking that Ninject is trying to access the role provider too soon.

In my NinjectWebCommon class it appears that it's using WebActivator to start itself. [assembly: WebActivator.PreApplicationStartMethod(typeof(Admin.App_Start.NinjectWebCommon), "Start")]

It appears that all of the articles I've come across are using older versions of Ninject and are doing a lot of the heavy lifting in the Global.asax Application_Start... Using my implementation how can I get DI working for a custom role provider?

I'm using the [Inject] attribute within my custom provider.

Community
  • 1
  • 1
The Muffin Man
  • 19,585
  • 30
  • 119
  • 191

1 Answers1

1

The WebActivator pipeline runs way before even the standard ASP.NET stack is created. It means that you won't have access to anything created by ASP.NET during bootstrap in NinjectWebCommon.

Use that file only to declare bindings that do not depend on ASP.NET stuff to be up. In order to get around this issue, you should use Global.asax Application_Start handler to load any additional modules/bindings that are dependend on ASP.NET stuff such as Roles.Provider.

Here is a suggestion that may solve your problem:

public void Application_Start()
{
    var kernel = (new Bootstrapper()).Kernel;
    kernel.Inject(Roles.Provider);

    //Other initialization stuff
}

The Bootstrapper class is a lazy singleton that has a static IKernel initialized within your NinjectWebCommon.cs. So this is the proper way of retrieving the configured kernel instance from outside your NinjectWebCommon.

Give it a try.

cvbarros
  • 1,684
  • 1
  • 12
  • 19