Ninject by introducing .Provider membership in RegisterServices initializing ninject
Does anyone know how to set up Memberhip.Provider in RegisterServices of ninject init 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 bound, except for the kernel. Input of strings. I get an error: "This method cannot be called during the initialization phase before starting the application."
The file is the NinjectWebCommon of the standard nuget installation ninject.mvc. I am trying to create a custom membership provider and inject a service layer (SecurityService) into the provider.
Any help?
source to share
I hit my head against the wall with this same problem, I finally found a relatively clean solution here .
Basic approach:
-
Use the attribute
[Inject]
on any dependency properties in your custom membership provider class. -
Create an http module to resolve / inject your vendors immediately (Ninject 3 supports HttpModules injection).
-
Replace those last 2
inject
statements in your methodRegisterServices()
with provider bindings and a new HttpModule.kernel.Bind<MembershipProvider>().ToMethod(ctx => Membership.Provider);
kernel.Bind<IHttpModule>().To<ProviderInitializationHttpModule>();
source to share