4

With the latest version of Unity (MVC5) the InjectionFactory has become deprecated. Below is the Obsolete warning you will receive when trying to use it.

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

Unfortunately I lack the knowledge with this API to put in the appropriate fix.

As you can see from the code below I'm trying to register an IAuthenticationManager using the old solution which leverages InjectionFactory. Does anyone know how this would look with the new solution?

public static void RegisterComponents()
{
    var container = new UnityContainer();

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

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

Below I've also included a controller which references this object.

public class AccountController : Controller
{
    private AdAuthenticationService _signInService;

    public AccountController() { }

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

    etc...

Let me know if you all have any other questions and thanks for the help.

1 Answer 1

15

I feel a little stupid. I took the time to actually read the warning and the answer was right there.

One line replacement:

Old:

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

New:

container.RegisterFactory<IAuthenticationManager>(c => HttpContext.Current.GetOwinContext().Authentication);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.