1

I am trying to setup Unity for the first time in a WebApi project. I have added Unity.WebApi from Nuget and my UnityConfig file looks like this.

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

        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        container.RegisterType<ApplicationDbContext>(
            new InjectionFactory(c => new ApplicationDbContext()));

        //container.RegisterType<ApplicationSignInManager>();
        container.RegisterType<ApplicationUserManager>();

        GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);

    }

However, if I try and call one of the default controllers, Account/Register I get an error saying ensure that I have a parameterless constructor.

I have looked at various articles that explain that the way Unity works in MVC and WebApi but as far as I can tell my config is correct? Im guessing Im missing something simple here as my installation works perfectly for constructor injection in an MVC project.

1 Answer 1

1

Unity will use the most greedy constructor (constructor with maximum number of parameters) when it tries to resolve your dependency, so in the case of the AccountController it has two constructors first one is parameterless and the second is with two parameters like bellow, and this what unity will try to use.

public AccountController(ApplicationUserManager userManager,
            ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
        {
            UserManager = userManager;
            AccessTokenFormat = accessTokenFormat;
        }

To override this, decorate required constructor with InjectionConstructorAttribute.

Hope that helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I actually had some success by using this directly on the controller:- container.RegisterType<AccountController>(new InjectionConstructor(typeof(ApplicationUserManager))); Is this an acceptible way of doing things with Unity? Secondly I am really struggling with generating a /Token. Again, the OAuthProvider class that comes out of the box only takes one parameter (publicClientId) but I cant seem to inject UserManager into to it. Any suggestions?
This link dropbox.com/s/j0c0qfoqjllty80/TestWebApi2WithUnity.zip?dl=0 has a Web API 2 project with Unity setup as a dependency injection, and all what I did after I created a new Web API project was to installing Unity.AspNet.WebApi nuget packages, and later decorate the AccountController parametless constructor by [InjectionConstructor] attribute to force unity to use this parametless constructor. and it worked fine, so hopefully that will help.
Thanks Omar.Alani will check this out. The problem I am having is that I have moved all the Identity objects and applicationDbContext to a Domain layer (So in theory, all the OWIN/OAuth stuff should be the only things in my presentation layers). This seems to give me issues as I cannot inject the UserManager (now in the domain layer) to the OAuth Provider. Ive just added this to another question as it is getting more specific now. stackoverflow.com/questions/27041112/…

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.