当前类型Microsoft.AspNet.Identity.IIdentityMessageService是一个接口,无法构造

时间:2016-11-15 05:32:01

标签: c# asp.net-mvc unity-container

我有一些注射问题 - 当我设置我的单元测试项目时崩溃了。它转了一圈,我在注册中命名了实例,这可能是也可能不是那么干净但是我觉得它给了更多可读性。

我有一个UsersController,用于管理Microsoft.AspNet.Identity域中的用户和角色。它构造如下:

class {
    [InjectionConstructor]
    public UsersController(
        [Dependency("userManager")] UserManager userManager,
        [Dependency("signInManager")] ApplicationSignInManager signInManager,
        [Dependency("roleManager")] ApplicationRoleManager roleManager,
        [Dependency("emailService")] IIdentityMessageService emailService)
    {
        RoleManager = roleManager;
        UserManager = userManager;
        SignInManager = signInManager;
        EmailService = emailService as EmailService;
        Trace.WriteLine("constructed");
    }

    public EmailService EmailService { get; private set; }

    public ApplicationSignInManager SignInManager { get; private set; }

    public UserManager UserManager { get; }

    public ApplicationRoleManager RoleManager { get; }
}

我的Unity容器中的引导程序如下:

container
.RegisterType<IdentityDb>("identityDb", new InjectionConstructor("DefaultConnection"))
.RegisterType<RoleStore<IdentityRoleIntPk, int, UserRoleIntPk>, ApplicationRoleStore>("roleStore", new InjectionConstructor(new ResolvedParameter<IdentityDb>("identityDb")))
.RegisterType<UserStore<ApplicationUser, IdentityRoleIntPk, int, UserLoginIntPk, UserRoleIntPk, UserClaimIntPk>, ApplicationUserStore>("userStore", new InjectionConstructor(new ResolvedParameter<IdentityDb>("identityDb")))
.RegisterType<RoleManager<IdentityRoleIntPk, int>, ApplicationRoleManager>("roleManager")
.RegisterType<IIdentityMessageService, EntityEmailService>("emailService")
.RegisterType<IAuthenticationManager>("authenticationManager",
                new InjectionFactory(c =>
                    HttpContext.Current.GetOwinContext().Authentication))
.RegisterType<SignInManager<ApplicationUser, int>, ApplicationSignInManager>("signInManager")
.RegisterType<IdentityFactoryOptions<UserManager>>("identityFactoryOptions",
                new InjectionFactory(c =>
                    new IdentityFactoryOptions<UserManager>()
                    {
                        DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("pacientio.Application")
                    }))
.RegisterType<UserManager<ApplicationUser, int>, ApplicationUserManager>("userManager");

这就是堆栈跟踪的样子

  

[ResolutionFailedException:依赖关系的解析失败,输入=&#34; pacientio.client.Controllers.AccountController&#34;,name =&#34;(none)&#34;。   在解决时发生异常。

     

异常是:InvalidOperationException - 当前类型Microsoft.AspNet.Identity.IIdentityMessageService是一个接口,无法构造。你错过了类型映射吗?

     

在例外时,容器是:

Resolving .AccountController,(none)
Resolving parameter "userManager" of constructor AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager, ApplicationRoleManager roleManager)

  Resolving ApplicationUserManager,userManager
  Resolving parameter "emailService" of constructor ApplicationUserManager(pacientio.entities.IdentityConfig.ApplicationUserStore userStore, Microsoft.AspNet.Identity.Owin.IdentityFactoryOptions`1[[pacientio.entities.IdentityConfig.UserManager, pacientio.entities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] options, Microsoft.AspNet.Identity.IIdentityMessageService emailService)
    Resolving Microsoft.AspNet.Identity.IIdentityMessageService,emailService

我的问题似乎源于:.RegisterType<IIdentityMessageService, EntityEmailService>("emailService")用户管理员用来发送登录/密码邮件等。

所以这里也是UserManager构造函数

public class ApplicationUserManager: UserManager
{
    public ApplicationUserManager(
        [Dependency("userStore")] ApplicationUserStore userStore,
        [Dependency("identityFactoryOptions")] IdentityFactoryOptions<UserManager> options,
        [Dependency("emailService")] IIdentityMessageService emailService)
        : base(userStore, options, emailService)
    {
    }
}

问题是..为什么恶魔,没有映射IIdentityMessageService mapTo EmailService启动?我明确地将其注册到RegisterType<IIdentityMessageService, EmailService>("emailService")并通过[Dependency("emailservice")]

提出要求

0 个答案:

没有答案