使用Unity

时间:2016-08-12 15:07:51

标签: c# unity-container

我正在尝试向Microsoft Unity注册ISecureDataFormat。我试着像这样注册:

container.RegisterType<ISecureDataFormat<AuthenticationTicket>>();

但是当我尝试上课时遇到以下错误:

  

当前类型,Microsoft.Owin.Security.ISecureDataFormat`1 [Microsoft.Owin.Security.AuthenticationTicket],   是一个接口,无法构造。你错过了一个类型吗?   映射

Microsoft没有DI的原始代码如下所示:

public AccountController(ApplicationUserManager userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
    UserManager = userManager;
    AccessTokenFormat = accessTokenFormat;
}

public ApplicationUserManager UserManager
{
    get
    {
        return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}

public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }

也许我需要使用InjectionFactory或其他东西,但此刻我被卡住了。

1 个答案:

答案 0 :(得分:3)

最后让它发挥作用:

container.RegisterType(typeof(ISecureDataFormat<>), typeof(SecureDataFormat<>));
container.RegisterType<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>();
container.RegisterType<ISecureDataFormat<AuthenticationTicket>, TicketDataFormat>();
container.RegisterType<IDataSerializer<AuthenticationTicket>, TicketSerializer>();
container.RegisterType<IDataProtector>(
    new InjectionFactory(c => new DpapiDataProtectionProvider().Create("ASP.NET Identity")));