I’m trying to use Ninject to provide my custom MembershipProvider with an instance of the (default) AspNetSqlMembershipProvider:
Bind<IMembershipService>()
.To<MembershipService>()
.WithConstructorArgument("provider", System.Web.Security.Membership.Provider));
I’m instantiating my Ninject module using the standard MVC3 pattern in the App_Start, specifically I am invoking the static Start() method using the WebActivator
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.App_Start.NinjectMVC3), "Start")]
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
bootstrapper.Initialize(CreateKernel);
}
The problem I have is that at this point, the default System.Web.Security.Membership.Provider as not been established and the application errors on start-up.
To get over this problem, I have restructured the code by adding a WebActivator.PostApplicationStartMethod:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MyProject.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.PostApplicationStartMethod(typeof(MyProject.App_Start.NinjectMVC3), "PostStart")]
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
}
and moving the bootstrapperInitialze() call into a PostStart() method:
public static void PostStart()
{
bootstrapper.Initialize(CreateKernel);
}
This resolves the problem, but I was wondering if a) this is likely to introduce other problems, or b) if there was an alternative approach?
ADDITIONAL DETAILS:
I have tried modifying the bindings as suggested, but I get the following compilation error:
‘Ninject.Syntax.BindingRoot.Bind()’ is a ‘method’, which is not valid in the given context
For information, here is the listing for my MembershipModule:
public class MembershipModule : NinjectModule
{
public override void Load()
{
// Membership Service
Bind<Abl.Mvc.Models.Membership.Abstract.IMembershipService>()
.To<Abl.Mvc.Models.Membership.Services.MembershipService>()
.WithConstructorArgument("provider", System.Web.Security.Membership.Provider)
.WithConstructorArgument("connectionString", MembershipProvider.GetConnectionString());
// This line errors
Bind<MembershipProvider>.ToMethod(ctx => System.Web.Security.Membership.Provider);
}
}
Thank you for your help.
Change your bindings to: