我正在创建一个具有自定义成员资格提供程序的 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 依赖注入与 Ninject 3.0.0的博客文章,以及相关的 StackExchange 讨论,以及关于 NamedLikeFactoryMethod和许多其他帖子的这个问题,但我被卡住了。我错过了什么?