1

I'm trying to configure Unity IoC in ASP.NET Web Api project. I have made installation of Unity.WebAPI package:

PM > Install-Package Unity.MVC4

And added the next code to Initialise method of Bootstrapper class:

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

Also I have registered my repository in container:

container.RegisterType<IProductRepository, ProductRepository>();

But my App crashes with the error Type 'MvcApplication1.Controllers.ValuesController' does not have a default constructor. Could someone point me out what have I missed? I use .Net Framework 4.5. Which package of Unity is OK for my purposes (Unity.WebAPI, Unity.MVC4, ...)?

Updated: Injection of repository into controller:

private readonly IProductRepository _repository;

public ValuesController(IProductRepository repository)
{
    _repository = repository;
}
5
  • i think you should add your code of ValuesController as well as it seems the error is in conjunction with that, what methods are there inside that controller, which one did you (try to) call? Commented Nov 15, 2013 at 15:04
  • Oops! I'm sorry, just updated. Commented Nov 15, 2013 at 15:14
  • wait a second! First you say I have made installation of Unity.WebAPI, but then you install Unity.MVC4 which one was it now, what are you building? MVC application or Web API application? Commented Nov 15, 2013 at 15:28
  • I'm working on Web API project, but installed package is Unity.MVC4. Actually I've tried to install Unity.WebAPI but after installation I found compile errors: 'MvcApplication1.Areas.HelpPage.XmlDocumentationProvider' does not implement interface member 'System.Web.Http.Description.IDocumentationProvider.GetResponseDocumentation(System.Web.Http.Controllers.HttpActionDescriptor)' ...\MvcApplication1\Areas\HelpPage\XmlDocumentationProvider.cs As a result I have no idea how to workaround it... Commented Nov 15, 2013 at 15:41
  • did you try what i suggested you in my answer? Commented Nov 15, 2013 at 16:04

2 Answers 2

1

What you did looks good so far, but I think you are missing just this one line

Bootstrapper.Initialise();

This should go into the startup code of your application Global.asax.cs for MVC-Applications for example, just put it there at the end inside of this method

protected void Application_Start()
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! Thank you, it was pretty funny omission :)
0

Probably you are following this article. If not, it could help you out.

http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver

Regarding the error, try adding a constructor to the controller with no parameters:

i.e.

public ValuesController()
{

}

3 Comments

I don't need parameterless constructor because I have to inject the repository into controller...
My apologies, you are correct. I just mentioned the parameterless constructor because it looks like that somewhere in the code is expected to be available the default constructor of the controller, which is a constructor with no parameters.
@emendezrivas Just to add some clarification to that. What you describe is true for the default behavior. Unity's IoC however should override that behavior in the sense that Unity will always try to match the constructor with the biggest number of parameters. So in every other case you would have been right, but when it comes to IoC you have to "switch" in your head that the default behavior should be different => i.e. configuration error.

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.