Ninject with MembershipProvider | RoleProvider

本小妞迷上赌 提交于 2019-11-29 20:57:41

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4650155/ninject-with-membershipprovider-roleprovider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!