17

Problem: I'm using MVC4 WebAPI and am throwing an error during a Get() call.

Error:

System.ArgumentException: Type 'Comments2.Controllers.CommentsController' does not have a default constructor

StackTrace:

at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}

I'm happy to give any code required simply let me know what you'd like to see.

Controller:

namespace Comments2.Controllers 
{
    //[Authorize]
    public class CommentsController : ApiController 
    {
        ICommentRepository repository;

    public CommentsController(ICommentRepository repository) 
    {
        this.repository = repository;
    }

    [Queryable]
    public IQueryable<Comment> GetComments()
    {
        return repository.Get().AsQueryable();
    }

    public Comment GetComment(int id)
    {
        Comment comment;
        if (!repository.TryGet(id, out comment))
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        return comment;
    }
}

JavaScript:

$(function() {
    $("#getComments").click(function () {
        // We're using a Knockout model. This clears out the existing comments.
        viewModel.comments([]);

        $.get('/api/comments', function (data) {
            // Update the Knockout model (and thus the UI) with the comments received back 
            // from the Web API call.
            viewModel.comments(data);
        });

    });
});
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Computer Guy
  • 411
  • 2
  • 9
  • 21
  • 1
    Did you properly set up a DI container, and launched it from the application start? Did you configure an instance of ICommentRepository to inject? – Leon Cullens Jul 15 '12 at 21:39
  • I have not. Would it be better to user Unity or Ninject? Those are the only two I'm interested in using, I understand the concept of IoC and DI but I'm trying to learn to use it with MVC4 and WebAPI ...do I just add that via NuGet? – Computer Guy Jul 15 '12 at 21:47

2 Answers2

8

It seams like you are using default implementation of HttpControllerActivator which will not work with dependency injection. Try this it integrates unity container to handle dependency but you can modify it to use any implementation of DI you want.

Community
  • 1
  • 1
Rafal
  • 12,391
  • 32
  • 54
  • Why a down vote? DefaultHttpControllerActivator simply requires default constructor so you have to create your own and the cleanest solution to this is DI container. – Rafal Jul 16 '12 at 04:01
  • The link provided in Rafal's answer help guide me in the right direction. – Computer Guy Jul 16 '12 at 05:52
  • As links may die, SO encourage answers to include the relevant information for a solution in the answer. If the linked content is removed or changed the answer will be useless for others who finds this later on. That might be why someone down voted (not me), although it's bad form to down vote without commenting on why. – Zaphod Jan 12 '17 at 18:17
  • @Zaphod, true yet its internal SO link just the same as you get if question is closed as duplicate. – Rafal Jan 13 '17 at 07:43
  • @Rafal I didn't says it was right, just why it probably was down voted :-) Internal SO links are ok in my opinion... – Zaphod Jan 13 '17 at 08:34
1

I'm not sure what IOC Container you are using, I personally use Ninject and here are the instructions I use to get this working properly.

bkorzynski
  • 541
  • 1
  • 5
  • 15