1

I am trying to get Unity Container Dependency Injection working on a self-hosted owin app. I have added the Unity nuget package to my project, and have set up my UnityConfig class as follows:

public static class UnityConfig
{
    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<IDBContext,UniversalCoatingsDbContext>();
        container.RegisterType<IUserRepository,UserRepository>();

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

Then, I set up my controller like this:

public class UserController : ApiController
{
    private IUserRepository userRepo;

    public UserController() {
        Console.WriteLine("User controller default constructor");
    }

    //dependency injected value
    public UserController(IUserRepository repo)
    {
        this.userRepo = repo;
        Console.WriteLine("DI enabled constructor");
    }

    [HttpPost]
    public JsonResult<MessageResponse> Register(UnhashedUser unhashed_user)
    {
        MessageResponse response = new MessageResponse();
        if(ModelState.IsValid)
        {
            response = userRepo.createUser(unhashed_user);
        }
        else
        {
            response.message = "Invalid Request sent.";
        }

        return Json(response);

    }

}

The UnityConfig.RegisterComponents() method is called at the end of my Startup class's Configuration() method (after all the middleware is setup). When I access this controller's URL, the parameterless constructor is always called, and thus the instance of IUserRepository is never resolved.

Am I missing some sort of configuration pattern? Any info greatly appreciated.

6
  • 1
    Try to remove the default constructor Commented Jul 23, 2016 at 20:10
  • If i do I get a "no default constructor" exception Commented Jul 23, 2016 at 21:26
  • Try to make it private Commented Jul 23, 2016 at 21:26
  • 3
    Are you sure that you are setting the correct DependencyResolver? Take a look at this: damienbod.com/2013/10/01/self-host-webapi-with-owin-and-unity Commented Jul 23, 2016 at 21:35
  • Making the default constructor private made no difference (it still gets called. I know because the Console.WriteLine() statement in it is executing. Commented Jul 24, 2016 at 12:38

1 Answer 1

1

I modified my UnityConfig class to expose the container as a static variable. And then,

I added this to my startup class:

        //set up dependency injection
        UnityConfig.RegisterComponents();
        config.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(UnityConfig.container);
        app.UseWebApi(config);
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.