asp.net-mvc - 使用 Unity MVC5 进行依赖注入 - 不推荐使用 InjectionFactory
问题描述
在最新版本的 Unity (MVC5) 中,InjectionFactory 已被弃用。以下是您在尝试使用它时会收到的过时警告。
[Obsolete("InjectionFactory has been deprecated and will be removed in next release. Please use IUnityContainer.RegisterFactory(...) method instead.", false)]
不幸的是,我缺乏使用此 API 的知识来进行适当的修复。
正如您从下面的代码中看到的那样,我正在尝试使用利用 InjectionFactory 的旧解决方案注册 IAuthenticationManager。有谁知道新解决方案的效果如何?
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}
下面我还包含了一个引用该对象的控制器。
public class AccountController : Controller
{
private AdAuthenticationService _signInService;
public AccountController() { }
public AccountController(IAuthenticationManager signInManager)
{
this._signInService = new AdAuthenticationService(signInManager);
}
etc...
如果大家还有其他问题,请告诉我,并感谢您的帮助。
解决方案
我觉得有点傻。我花时间真正阅读了警告,答案就在那里。
一行替换:
老的:
container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
新的:
container.RegisterFactory<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);
推荐阅读
- javascript - 如何使用 vanilla js 显示输入值?
- javascript - React:我可以在 onChange 事件函数中使用异步吗?
- ios - Chartboost iOS 8.2.0 SDK 的 GDPR 许可
- spring-boot - 在同一应用程序中使用内存中的 H2 和 H2 文件
- javascript - 带增量的倒计时计时器
- flutter - 如何更新可见的 ScrollablePositionedList 项目
- batch-file - 使用批处理文件将文件夹移动到按字母顺序编目的文件夹
- keras - 训练时在keras中保存前n个模型
- mongodb - 如何使用 MongoDB 在数组中投影子文档值?
- python - 将列表的元素分成其中的许多元素并通过条件选择其中一个元素