我正在创建一个具有自定义成员资格提供程序的ASP.NET MVC 3 webapp。我正在使用Ninject.Extensions.Factory 3.0.0
尝试将工厂注入我的自定义成员资格提供商的公共财产中,但它并不顺利。
这是我的自定义成员资格类和工厂界面:
public class MtgMembershipProvider : MembershipProvider
{
[Inject]
public IUnitOfWorkFactory UnitOfWorkFactory { get; set; }
...
}
public interface IUnitOfWorkFactory
{
IUnitOfWork GetEFUnitOfWork();
}
这是我的会员资格初始化模块:
public class MtgMembershipInitializationModule : IHttpModule
{
public MtgMembershipInitializationModule(MembershipProvider membershipProvider)
{
}
public void Dispose()
{
}
public void Init(HttpApplication context)
{
}
}
这是来自NinjectWebCommon.cs的RegisterServices代码:
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>();
}
在web.config中,我有以下内容:
<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>
当我启动webapp时,我在模块初始化期间遇到异常:
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.
我不确定我做错了什么。我已经关注了Remo Gloor关于ASP.NET Provider dependency injection with Ninject 3.0.0和related StackExchange discussion以及this question about NamedLikeFactoryMethod和其他许多帖子的博文,但我被困了。我错过了什么?
答案 0 :(得分:1)
你必须绑定你的工厂
Bind<IUnitOfWorkFactory>().ToFactory();