我正在使用MVC 5 EF,Identity,Unity清理旧的应用程序,但无法弄清楚如何为AccountController注册DI。在里面
在下面的示例中,我更改了
下面的空构造函数

      public AccountController()
    { }


对此

public AccountController(IDateTimeService dateTimeService, ILeagueLogic leagueLogic)
{
    _dateTimeService = dateTimeService;
    _leagueLogic = leagueLogic;
}


所以在完整的控制器中,我有以下内容

  public class AccountController : Controller
{
    private ApplicationUserManager _userManager;
    private IDateTimeService _dateTimeService;
    private ILeagueLogic _leagueLogic;

         [InjectionConstructor] <-- Nailed it
    public AccountController(IDateTimeService dateTimeService, ILeagueLogic leagueLogic)
    {
        _dateTimeService = dateTimeService;
        _leagueLogic = leagueLogic;
    }

    public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

  private ApplicationSignInManager _signInManager;
    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set { _signInManager = value; }
    }


    //
    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }


}

在UnityConfig文件中,我有以下内容

 public static void RegisterTypes(IUnityContainer container)
    {

        container.RegisterType<IDateTimeService, DateTimeService>();
        container.RegisterType<ILeagueLogic, LeagueLogic>();


        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
        container.RegisterType<UserManager<ApplicationUser>>();
        container.RegisterType<DbContext, ApplicationDbContext>();
        container.RegisterType<ApplicationUserManager>();
       // container.RegisterType<AccountController>(new InjectionConstructor());  <-- Removed this line
    }


但是我明白了


  MyProgram.Controllers.AccountController类型不具有采用参数()的构造函数。


Kinda陷入困境。我相信我需要以某种方式不确定是否注册控制器。

最佳答案

最后一行

container.RegisterType<AccountController>(new InjectionConstructor());

explicitly tells Unity使用AccountController构造函数创建new AccountController()实例

Based on the docs类型注册应如下所示:

    container.RegisterType<AccountController>(
               new InjectionConstructor(
                 container.Resolve< IDateTimeService>(),
                 container.Resolve< IDateTimeService >()));


UPD:如果您的AccountController类中只有一个构造函数,则可以尝试完全删除container.RegisterType<AccountController>(new InjectionConstructor());行。 Unity应该能够构造一个没有提示的实例。

UPD2:

如果您有多个构造函数,但是Unity仅使用一个构造函数来构造实例(例如,第二个可能用于单元测试项目中),则可以使用[InjectionConstructor]属性装饰新的ctor

[InjectionConstructor]
public AccountController(IDateTimeService dateTimeService, ILeagueLogic leagueLogic)
{
    _dateTimeService = dateTimeService;
    _leagueLogic = leagueLogic;
}

10-02 19:24