How to inject repositories into a custom Membershi

2019-08-09 10:23发布

问题:

It seem i can't get this to work. I made a custom MembershipProvider and i want to inject a repository inside but the [Inject] property just doesnt work.

  public class PyrosphereMembershipProvider : MembershipProvider
    {
        [Inject]
        protected IDepositoireUtilisateur DepositoireUtilisateur { get; set; }

        [Inject]
        protected IDepositoireProfile DepositoireProfile { get; set; }
...

I think this is because this class are created way before the MVC application is running which cause the actual problem. I am getting more problem trying to use the membershipprovider than making my own system.

About my ninject configuration i am using the App_Start directory method. All my binding are correct ive looked so that nothing to do with my bindings.

Any help is appreciated!

回答1:

The ASP.NET Providers aren't designed for proper dependency injection. There are only workarounds for them.

Either you have to setup a binding for them and resolve atleast one instance before using them. E.g. by injecting an instance into the Global.asax or you have to Inject an instance manually before the first use (kernel.Inject(...)). Again most likely in the global.asax application start method.