Ninject, injecting Membership.Provider in RegisterServices of ninject initialization

陌路散爱 提交于 2019-12-22 10:29:12

问题


Anybody know how to configure Membership.Provider in RegisterServices of ninject initialization code?

In my code:

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        // Put additional bindings here
        kernel.Bind<IDatabaseFactory>().To<DatabaseFactory>();
        kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
        kernel.Bind<IUserRepository>().To<UserRepository>();
        kernel.Bind<IRoleRepository>().To<RoleRepository>();
        kernel.Bind<ISecurityService>().To<SecurityService>();
        kernel.Inject(Membership.Provider);
        kernel.Inject(Roles.Provider);
    }

all modules are binded with the exception of kernel.Inject lines. I'm receiving the error: "This method cannot be called during the application's pre-start initialization stage".

The file is NinjectWebCommon of a standard nuget ninject.mvc installation. I'm trying to create a custom membership provider and injecting the service layer (SecurityService) in the provider.

Any help?


回答1:


I hit my head against the wall with this same issue, I finally found a relatively clean solution here.

The basic approach is:

  1. Use [Inject] attribute on any dependency properties in your custom membership provider class.

  2. Create a http module to resolve/inject your providers right away (Ninject 3 supports injecting HttpModules).

  3. Replace those last 2 inject statements in your RegisterServices() method with bindings for the provider and new HttpModule.

    kernel.Bind<MembershipProvider>().ToMethod(ctx => Membership.Provider); kernel.Bind<IHttpModule>().To<ProviderInitializationHttpModule>();



来源:https://stackoverflow.com/questions/10053289/ninject-injecting-membership-provider-in-registerservices-of-ninject-initializa

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