Trouble injecting factory into custom membership provider in ASP.NET MVC 3 with Ninject 3.0.0 with Factory extension

StackOverflow https://stackoverflow.com/questions/12753395

I am creating an ASP.NET MVC 3 webapp that has a custom membership provider. I am using Ninject.Extensions.Factory 3.0.0 to try to inject a factory into a public property on my custom membership provider, but it's not going smoothly.

Here is my custom membership class and factory interface:

public class MtgMembershipProvider : MembershipProvider
{
    [Inject]
    public IUnitOfWorkFactory UnitOfWorkFactory { get; set; }

    ...
}

public interface IUnitOfWorkFactory
{
    IUnitOfWork GetEFUnitOfWork();
}

Here is my membership initialization module:

public class MtgMembershipInitializationModule : IHttpModule
{
    public MtgMembershipInitializationModule(MembershipProvider membershipProvider)
    {
    }

    public void Dispose()
    {            
    }

    public void Init(HttpApplication context)
    {
    }
}

Here's the RegisterServices code from NinjectWebCommon.cs:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUnitOfWork>()
        .To<EFUnitOfWork>()
        .NamedLikeFactoryMethod((IUnitOfWorkFactory f) => f.GetEFUnitOfWork());

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

In web.config, I have the following:

<system.web>
  <membership defaultProvider="MtgMembershipProvider">
    <providers>
      <clear />
      <add name="MtgMembershipProvider" type="MtgManager.WebUI.Infrastructure.MtgMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="MtgManager_WebUI" />
    </providers>
  </membership>
</system.web>

When I launch the webapp, I get an exception during module initialization:

Error activating IUnitOfWorkFactory
No matching bindings are available, and the type is not self-bindable.
Activation path:
 3) Injection of dependency IUnitOfWorkFactory into property UnitOfWorkFactory of type MtgMembershipProvider
 2) Injection of dependency MembershipProvider into parameter membershipProvider of constructor of type MtgMembershipInitializationModule
 1) Request for IHttpModule

Suggestions:
 1) Ensure that you have defined a binding for IUnitOfWorkFactory.
 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
 3) Ensure you have not accidentally created more than one kernel.
 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
 5) If you are using automatic module loading, ensure the search path and filters are correct.

I'm not sure what I'm doing wrong. I've followed Remo Gloor's blog post about ASP.NET Provider dependency injection with Ninject 3.0.0, and the related StackExchange discussion, as well as this question about NamedLikeFactoryMethod and numerous other posts, but I am stuck. What am I missing?

有帮助吗?

解决方案

You have to bind your factory

Bind<IUnitOfWorkFactory>().ToFactory();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top