如何在WebApi中注入webapi AccountController [英] How to inject webapi AccountController in WebApi
问题描述
我有默认的构造函数和构造函数,其参数如下:
I have default constuctor and constructor with params like here:
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>();
但是事实是,总是会调用默认构造函数.
我读了这篇文章博客,但他们不谈论如何使用构造函数具有参数AccountController(ApplicationUserManager userManager, ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
But the thing is that default constructor is always getting called.
I read taht article blog but they not talking how to resolve such case with constructor havaving params 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.",
有没有人可以帮助您?
If I am taking off parameterless constructor I am getting an error: "An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.",
Is there anyone can help?
我也只有普通的ApiController,我也没有注射:
Also I have just normal ApiController and i is not getting injected as well:
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不是.我删除了默认构造函数,但仍然收到错误.
UPDATE 1 Based on @IgorPashchuk suggeston now MyController
is getting injected now.
But AccoutController is not. I am removed the default constructor but still getting the error.
更新2 我通过删除第二个参数来更改带有参数的构造函数:
UPDATE 2 I alter the constructor with params by taking out the second param:
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>
.我发布了有关此问题的另一个问题如何构造ISecureDataFormat< AuthenticationTicket>团结一致
So this way I've got the thing is working. Is I understand that is means that Unity wasnt able to construct type ISecureDataFormat<AuthenticationTicket>
. I post another question about this issue How to construct ISecureDataFormat<AuthenticationTicket> with unity
推荐答案
您应删除无参数构造函数.
You should remove the parameterless constructor.
接下来,您需要配置一个自定义的依赖解析器,该解析器将基本上包装您的Unity容器.参见 http://www.asp.net/web-api/overview/高级/依赖注入
Next, you need to configure a custom dependency resolver, which will basically wrap your Unity container. See http://www.asp.net/web-api/overview/advanced/dependency-injection
之后,请确保您注册了所有类型.例如,我没有看到ApplicationUserManager
的注册.您正在注册UserManager<ApplicationUser, int>
而不是ApplicationUserManager
,这是IoC容器将尝试解决的问题.
After that, make sure that you register all your types. For example, I don't see a registration for ApplicationUserManager
. You are registering UserManager<ApplicationUser, int>
but not ApplicationUserManager
, which is what the IoC container will attempt to resolve.
这篇关于如何在WebApi中注入webapi AccountController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!