7

I'm using ninject as my IoC and I wrote a role provider as follows:

public class BasicRoleProvider : RoleProvider
{
    private IAuthenticationService authenticationService;

    public BasicRoleProvider(IAuthenticationService authenticationService)
    {
        if (authenticationService == null) throw new ArgumentNullException("authenticationService");
        this.authenticationService = authenticationService;
    }

    /* Other methods here */
}

I read that Provider classes get instantiated before ninject gets to inject the instance. How do I go around this? I currently have this ninject code:

Bind<RoleProvider>().To<BasicRoleProvider>().InRequestScope();

From this answer here.

If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(MemberShip.Provider) - this will assign all dependencies to your properties.

I do not understand this.

Community
  • 1
  • 1
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • possible duplicate of [Inject repository to custom membership provider with Ninject](http://stackoverflow.com/questions/5596441/inject-repository-to-custom-membership-provider-with-ninject) – Ruben Bartelink Aug 22 '12 at 21:21

1 Answers1

9

I believe this aspect of the ASP.NET framework is very much config driven.

For your last comment, what they mean is that instead of relying on constructor injection (which occurs when the component is being created), you can use setter injection instead, e.g:

public class BasicRoleProvider : RoleProvider
{
  public BasicRoleProvider() { }

  [Inject]
  public IMyService { get; set; }
}

It will automatically inject an instance of your registered type into the property. You can then make the call from your application:

public void Application_Start(object sender, EventArgs e)
{
  var kernel = // create kernel instance.
  kernel.Inject(Roles.Provider);
}

Assuming you have registered your role provider in the config. Registering the provider this way still allows great modularity, as your provider implementation and application are still very much decoupled.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • I tried `kernel.Inject(Roles.Provider)` and `kernel.Inject(Membership.Provider)` – Shawn Mclean Jan 11 '11 at 18:35
  • Hmm, I've tested this with `kernel.Inject(Roles.Provider)`. Can you update your question with your config code and your RoleProvider code where you are trying to inject. Can you check you've set your RoleProvider as default too. – Matthew Abbott Jan 11 '11 at 19:00
  • Excellent! This worked after I got my namespaces right in the web.config. – Quesi Jan 12 '11 at 16:25
  • @Lol coder, it works for me. Did you also Bind `IAuthenticationService` to something? I noticed in your question you only mentioned `Bind().To().InRequestScope();` – Matthijs Wessels Mar 10 '11 at 15:35
  • Instead of hooking up your kernel in app start, I'd highly recommend the ninject.web.mvc nuget package. – viggity Apr 10 '13 at 03:22
  • @viggity can you tell us more about this, as i have n-tier application and ninject.web.mvc in main app, while only ninject in other tiers. And my membership provider is in other project. And i am facing same issue. – Zia Ul Rehman Mughal Apr 07 '16 at 14:28
  • @ZiaUlRehmanMughal my comment was more about how to most easily get ninject hooked up with MVC, I don't inject the role provider via ninject. I let the webconfig do that. I've put some sample code in this gist: https://gist.github.com/vongillern/f1f7ef0f0318f881ee9b4d4d750841c8#file-web-config – viggity Apr 07 '16 at 15:19