Como injetar webapi AccountController no WebApi
Eu tenho o construtor e construtor padrão com parâmetros como aqui:
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;
}
}
E a minha unidade de configuração aqui:
.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>();
Mas o problema é que o construtor padrão está sempre sendo chamado. Eu li esse artigoblogue mas eles não estão falando sobre como resolver esse caso com construtores que possuem parâmetrosAccountController(ApplicationUserManager userManager, ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
Se estou retirando o construtor sem parâmetros, estou recebendo um erro:"An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.",
Alguém pode ajudar?
Também tenho ApiController normal e não estou sendo injetado também:
public class MyController : ApiController
{
[Dependency]
public IRepository Repository { get; set; }
public IHttpActionResult Get()
{
var test = Repository.GetSomething(); // Repository is null here always
}
}
ATUALIZAÇÃO 1 Com base em @IgorPashchuk Suggeston nowMyController
está sendo injetado agora. Mas o AccoutController não é. Eu removi o construtor padrão, mas ainda estou recebendo o erro.
ATUALIZAÇÃO 2 Altero o construtor com parâmetros removendo o segundo parâmetro:
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;
}
}
Então, dessa maneira, eu entendi o que está funcionando. Entendo que isso significa que o Unity não conseguiu construir o tipoISecureDataFormat<AuthenticationTicket>
. Postei outra pergunta sobre esse problemaComo construir ISecureDataFormat <AuthenticationTicket> com unidade