【问题标题】:Dependency Injection with Unity MVC5 - InjectionFactory Deprecated使用 Unity MVC5 进行依赖注入 - InjectionFactory 已弃用
【发布时间】:2019-09-20 14:31:08
【问题描述】:

在最新版本的 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...

如果大家还有其他问题,请告诉我,感谢您的帮助。

【问题讨论】:

    标签: asp.net-mvc dependency-injection owin


    【解决方案1】:

    我觉得有点傻。我花时间真正阅读了警告,答案就在那里。

    一行替换:

    旧:

    container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
    

    新功能:

    container.RegisterFactory<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-28
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2015-04-22
      • 2015-09-26
      相关资源
      最近更新 更多