2
votes

I am trying to self bind MembershipProvider in ASP.NET MVC 2 and then use this binding in an AccountController constructor.

This is a snippet from my global.asax.cs

// selfbind MembershipProvider in request scope
Bind<MembershipProvider>().ToSelf().InRequestScope();

And a snippet from service class:

public AccountMembershipService(MembershipProvider provider, IAccountRepository accountRepository)
{
    _provider = provider ?? Membership.Provider;
    _accountRepository = accountRepository;
}

My problem is that injection isn't working (the injection for AccountRepository does work however). This is the error from Ninject:

Error activating MembershipProvider using self-binding of MembershipProvider
No constructor was available to create an instance of the implementation type.
Activation path:
   3. Injection of dependency MembershipProvider into parameter provider of constructor of type AccountMembershipService
   2. Injection of dependency IMembershipService into parameter membershipService of constructor of type AccountController
   1. Request for IController

Suggestions:
 1) Ensure that the implementation type has a public constructor.
 2) If you have implemented the Singleton pattern, use a binding with InSingletonScope() instead.

Setting InSingletonScope() makes no difference and I cannot do anything about the constructor since this is not a custom MembershipProvider but the default one that comes with ASP.NET.

I'm stuck here, don't know how to solve this.

2

2 Answers

4
votes

Ok, found it. I was mistakenly setting MembershipProvider to self, which couldn't work because MembershipProvider is abstract class.

This works

Bind<MembershipProvider>().ToConstant(Membership.Provider);
2
votes

You need to create a ctor on the membership provider that is empty and one with the repository on it. This is assuming you have registered the Membership Provider with the kernel correctly too...

    Bind<MembershipProvider>().To<YourMembershipProvider>();
    Bind<IMembershipService>().To<AccountMembershipService>();