gpt4 book ai didi

c# - 在 AccountController 中使用依赖注入(inject)的问题

转载 作者:太空宇宙 更新时间:2023-11-03 20:58:52 24 4
gpt4 key购买 nike

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

      public AccountController()
{ }

为此

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

所以在完整的 Controller 中我有以下

  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
}

但是我明白了

The type MyProgram.Controllers.AccountController does not have a constructor that takes the parameters ().

有点难过.,.我相信我需要以某种方式注册 Controller ,但不确定......具体如何

最佳答案

最后一行

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] 装饰新的构造函数属性

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

关于c# - 在 AccountController 中使用依赖注入(inject)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47761537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com