如何在 WebApi 中注入 webapi AccountController

How to inject webapi AccountController in WebApi

我有默认构造函数和带参数的构造函数,如下所示:

public class AccountController : ApiController
{
    private const string LocalLoginProvider = "Local";
    private ApplicationUserManager _userManager;
    public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }
    [Dependency]
    public IRepository Repository{ get; private set; }

    public AccountController()
    {
    }

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

这里是我的统一配置:

.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<UserManager<ApplicationUser, int>, ApplicationUserManager>()                
.RegisterType<ApplicationDbContext>(new HierarchicalLifetimeManager())
.RegisterType<ApplicationUserManager>()

.RegisterType<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>()
.RegisterType<ITextEncoder, Base64UrlTextEncoder>()
.RegisterType<IDataSerializer<AuthenticationTicket>, TicketSerializer>()
//.RegisterType<IDataProtector>(() => new DpapiDataProtectionProvider().Create("ASP.NET Identity"))

.RegisterType<IUserStore<ApplicationUser, int>, CustomUserStore>(new InjectionConstructor(typeof(ApplicationDbContext)))
.RegisterType<IAuthenticationManager>(new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication))
.RegisterType<IOwinContext>(new InjectionFactory(o => HttpContext.Current.GetOwinContext()))
.RegisterType<IRepository, Repository>();

但问题是默认构造函数总是被调用。 我读了一篇文章 blog 但他们没有讨论如何使用具有参数的构造函数解决这种情况 AccountController(ApplicationUserManager userManager, ISecureDataFormat<AuthenticationTicket> accessTokenFormat)

如果我取消无参数构造函数,我会得到一个错误:"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.", 有没有人可以帮忙?

此外,我只有普通的 ApiController,我也没有被注入:

public class MyController : ApiController
{
    [Dependency]
    public IRepository Repository { get; set; }

    public IHttpActionResult Get()
    {
        var test = Repository.GetSomething(); // Repository is null here always
    }
}

更新 1 基于@IgorPashchuk 建议现在 MyController 正在注入。 但 AccoutController 不是。我删除了默认构造函数但仍然收到错误。

更新 2 我通过删除第二个参数来改变带有参数的构造函数:

public class AccountController : ApiController
{
    private const string LocalLoginProvider = "Local";
    private ApplicationUserManager _userManager;

    [Dependency]
    public IRepository Repository{ get; private set; }

    public AccountController(ApplicationUserManager userManager)
    {
        _userManager = userManager;
    }
}

这样我就可以正常工作了。据我了解,这意味着 Unity 无法构造类型 ISecureDataFormat<AuthenticationTicket>。我post关于这个问题的另一个问题How to construct ISecureDataFormat<AuthenticationTicket> with unity

您应该删除无参数构造函数。

接下来,您需要配置一个自定义依赖解析器,它基本上会包装您的 Unity 容器。参见 http://www.asp.net/web-api/overview/advanced/dependency-injection

之后,确保注册所有类型。例如,我没有看到 ApplicationUserManager 的注册。您正在注册 UserManager<ApplicationUser, int> 而不是 ApplicationUserManager,这是 IoC 容器将尝试解决的问题。