Inject repository to custom membership provider with Ninject

后端 未结 4 2033
走了就别回头了
走了就别回头了 2020-11-30 10:13

I\'m trying to inject a repository to a custom membership provider with ninject in MVC 3.

In MembershipProvider I have tried the following:

[Inject]
         


        
4条回答
  •  旧巷少年郎
    2020-11-30 10:32

    i had this problem

    a custom membership, role and profile provider in another project from MVC using repository, when ever i call the provider the injected repository was null.

    tried to call kernel.Inject(Membership.Provider); in the NinjectWebCommon method registerServices(IKernel kernel) but got the exception

    The result is always null, because asp.net has it's own static property for membership.which is membership.provider. and this instance is not part of instance ninject management.

    so use on PostApplicationStartMethod

    here is the soloution by cipto add to NinjectWebCommon the attrbute and method :

        [assembly: WebActivator.PreApplicationStartMethod(typeof(WebApp.App_Start.NinjectWebCommon), "Start")]
        [assembly: WebActivator.PostApplicationStartMethod(typeof(WebApp.App_Start.NinjectWebCommon), "RegisterMembership")]
        [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(WebApp.App_Start.NinjectWebCommon), "Stop")]
    
        public static void RegisterMembership()
        {
            bootstrapper.Kernel.Inject(Membership.Provider);
        } 
    

提交回复
热议问题