Unity MVC5 依赖注入 - InjectionFactory 已弃用

spy*_*329 1 asp.net-mvc dependency-injection owin

在最新版本的 Unity (MVC5) 中,不推荐使用 InjectionFactory。以下是您在尝试使用它时将收到的过时警告。

[Obsolete("InjectionFactory has been deprecated and will be removed in next release. Please use IUnityContainer.RegisterFactory(...) method instead.", false)]
Run Code Online (Sandbox Code Playgroud)

不幸的是,我缺乏有关此 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));
}
Run Code Online (Sandbox Code Playgroud)

下面我还包含了一个引用此对象的控制器。

public class AccountController : Controller
{
    private AdAuthenticationService _signInService;

    public AccountController() { }

    public AccountController(IAuthenticationManager signInManager)
    {
        this._signInService = new AdAuthenticationService(signInManager);
    }

    etc...
Run Code Online (Sandbox Code Playgroud)

如果你们有任何其他问题,请告诉我,并感谢您的帮助。

spy*_*329 6

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

一行替换:

老的:

container.RegisterType<IAuthenticationManager>(new InjectionFactory(c => HttpContext.Current.GetOwinContext().Authentication));
Run Code Online (Sandbox Code Playgroud)

新的:

container.RegisterFactory<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);
Run Code Online (Sandbox Code Playgroud)