views:

59

answers:

2

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.

+1  A: 

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>();
CrazyDart
Yes, I do think your problem is with the kernel binding to the provider... try this: Bind<MembershipProvider>().To<YourMembershipProvider>().InRequestScope(); You should have no need to pass that repo, it should be injected into the MembershipProvider ctor... thats why you need an empty one and one with your repo on it.
CrazyDart
I don't have custom Membership provider at the moment. I want to use the default Membership.Provider provider. But still have everything ready if we implement custom provider in the future.
mare
Hrmmm, I think you should mark me for the answer as I was correct that the binding was bad.... and I could use the points.
CrazyDart
A: 

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);
mare