【问题标题】:How to inject webapi AccountController in WebApi如何在 WebApi 中注入 webapi AccountController
【发布时间】:2015-09-07 18:50:39
【问题描述】:

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

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&lt;AuthenticationTicket&gt; 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 正在被注入。 但 AcoutController 不是。我删除了默认构造函数,但仍然收到错误。

更新 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&lt;AuthenticationTicket&gt;。我发布了另一个关于这个问题的问题How to construct ISecureDataFormat<AuthenticationTicket> with unity

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • ApplicationUserManager 在那里:.RegisterType&lt;ApplicationUserManager&gt;()
    • 我还需要Unity.Mvc 包吗?好像我不再需要那个接缝了?
    • @sreginogemoh,你是对的。对不起,我错过了。你的第二个问题的答案是:这取决于。你不需要它来达到这个目的。但是,你肯定需要创建一个自定义依赖解析器,因为 Web API 不知道你的 Unity 容器。
    猜你喜欢
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-02
    • 2016-10-03
    • 2015-02-15
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多