如何在WebApi中注入webapi AccountController

时间:2015-09-07 18:50:39

标签: dependency-injection unity-container asp.net-web-api2

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

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
    }
}

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

UPDATE 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>。我发布了有关此问题的另一个问题How to construct ISecureDataFormat<AuthenticationTicket> with unity

1 个答案:

答案 0 :(得分:1)

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

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

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