MVC - IoC Unity - Make sure that the controller has a parameterless public constructor

16,248

Solution 1

I found it, the problem was that I am remaking existing code, in the same solution, meaning that there's going to be duplicate files with identical names.

In this particular case I had an interface that was exactly the same as a previously existing one. Somewhere deep in the code I used the wrong namespace, meaning that the IoC mapping was incorrect.

So, something like this:

I have:

old.managers.IMyManager

and

new.managers.IMyManager

What I was doing in the mapping was:

container.RegisterType<IMyManager, MyManager>();

where IMyManager in this case is new.managers.IMyManager, as it should be.

One of the consumers of this dependency expected IMyManager of namespace old.managers.

Updating the consumer to use the new namespace fixed it.

Solution 2

If the error says that you need a parameterless constructor, that suggests to me that Unity isn't registered for WebAPI, even though it is registered for your ASP.NET MVC application. If your IoC is working correctly, it shouldn't need a paramaterless constructor, because it should be able to resolve the dependencies that exist within your controller's constructor.

WebAPI has it's own pipeline to ASP.NET MVC for example WebAPI can be OWIN hosted, hence why it needs hooking up separately even if they exist in the same project.

You should hook your WebAPI to the dependency resolver within your UnityConfig, I believe that there is an example here: http://www.devtrends.co.uk/blog/using-unity.mvc5-and-unity.webapi-together-in-a-project

using Microsoft.Practices.Unity;
using System.Web.Http;
using System.Web.Mvc;

namespace WebApplication1
{
    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>();

            // Configures container for ASP.NET MVC
            DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));

            // Configures container for WebAPI
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
        }
    }
}

In the example above there are two lines for configuration, but they share the same container and thus the same bindings - you don't have to define them twice. If you ever came to separate the WebAPI project from the ASP.NET MVC one, then you would have to have a separate container and bindings.

In terms of finding the problem, or Unity hiding errors from you, in my experience this simply isn't the case. The error that you're experiencing is straight from ASP.NET MVC. In normal use without an IoC, you need a parameterless constructor... with an IoC it controls the creation of the controllers and thus resolves the dependencies.

Share:
16,248

Related videos on Youtube

Spikee
Author by

Spikee

Updated on September 23, 2022

Comments

  • Spikee
    Spikee over 1 year

    Problem

    I'm using Unity as IoC and that worked fine, but as I keep adding functionality, it becomes increasingly difficult to pinpoint any errors, because Unity provides an error that is the symptom of the error, not the actual error:

    "Message":"An error has occurred.","ExceptionMessage":"An error occurred
    when trying to create a controller of type 'MyController'. Make sure that the
    controller has a parameterless public
    constructor.","ExceptionType":"System.InvalidOperationException"
    

    Background

    I have an MVC Web Api controller that has a dependency on a Manager instance (from domain):

    public class MyController : ApiController
    {
        private IMyManager MyManager { get; set; }
    
        public MyController(IMyManager myManager)
        {
            this.MyManager = myManager;
        }
    
        ...
    }
    

    The above error occurs because IoC mapping of the IMyManager fails. As it fails, I have no parameter, meaning that MyController is called using a parameterless constructor, but since it doesn't exist (and shouldn't) I get the above error.

    What I have tried

    So, the error I get is not the 'real' error. The obvious thing is to make sure that every new implementation is registered under IoC, I checked and they are. I do it manually to keep things manageable (oh the irony!).

    I do that like this:

    container.RegisterType<IMyManager, MyManager>();
    

    But, that's not the issue.

    One fix I did was a circular dependency. I changed all involved constructors and methods to use property values, not instances. I checked all involved classes and there are no longer any circular dependencies.

    Yet, the problem still occurs.

    The question

    What can I do to find out the actual problem? Manually checking dependencies is way too much overhead, because the structure is an intricate web of deeper dependencies. And this will get worse as the app matures.

    Alternatively, if Unity just always obscures these messages (without being able to fix that), are there alternatives out there that do provide worthwhile error information?

    UPDATE

    Per request, the full error message (though I don't think the stacktrace is very helpful):

    {"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'MyController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":"   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
    at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Type 'MyProject.Web.Api.Controllers.MyController' does not have a default constructor","ExceptionType":"System.ArgumentException","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.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
    at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}
    

    UPDATE 2

    I dug a little deeper and checked all dependencies (they are pretty massive), but everything is registered and loaded correctly. There also aren't any circular dependencies, so as far as I can tell everything 'should' work. Since it doesn't I conclude that there's some error happening that is not being thrown up.

  • Luke
    Luke over 8 years
    @downvoter I'd appreciate a note so that I can improve the answer - thanks
  • Spikee
    Spikee over 8 years
    That's addressing the symptom, not the issue. And then you will get null reference errors because your dependencies are not being injected.
  • Spikee
    Spikee over 8 years
    I'm not the downvoter (I don't have the privilege yet) but it's not a good answer because I said IoC did work just fine before adding new functionality. There's 'something' going wrong in new development, not the existing code.
  • Luke
    Luke over 8 years
    @Spikee Please answer the following questions: How many WebAPI controllers do you have in your project? How many of them are currently working with dependency injection? Please detail how you are hooking your WebAPI controllers up to your Unity container. Then we can judge if this is a good answer or not...
  • Spikee
    Spikee over 8 years
    All controllers work with injected parameters, with instances being thrown in that have further dependencies forming a structure too complex to summarize here.
  • Luke
    Luke over 8 years
    Can you at least answer all of the questions. Bare in mind that WebAPI controllers are not the same as ASP.NET MVC controllers. I don't want details of the bindings, just how you are currently assigning your dependency resolver to WebAPI. Please add this to your question.
  • Ken Nguyen
    Ken Nguyen over 8 years
    In my project, I have two the constructors. public MyController(IMyManager myManager) { this.MyManager myManager; } and public MyController() {}. It work fine.
  • Spikee
    Spikee over 8 years
    How do you instantiate dependencies if the default constructor is used?
  • Luke
    Luke over 8 years
    This doesn't make sense, how would the namespace of an interface that you use in your bindings fix the error Make sure that the controller has a parameterless public constructor.? You must have changed the configuration.
  • Spikee
    Spikee over 8 years
    It's been a while but: If my constructor is expecting a parameter of type new.managers.IMyManager and my binding is done for old.managers.IMyManager, it's never going to arrive at the constructor because there's a significant difference in definition. It's just hard to notice at face value. A parameter that's not identified means the signature of the constructor wasn't matched. Meaning it looks for the default parameterless constructor. Which does not exist, on purpose.
  • Luke
    Luke over 8 years
    Strange, generally if this is the case, the IoC container will recognise that it was unable to bind an interface and produce a specific error message accordingly. The parameterless constructor error message is produced by the MVC framework itself when it can't create an instance of a controller.
  • Spikee
    Spikee over 8 years
    Come on man, this question hasn't been touched since october and yesterday I get a downvote at around the same time as your response. But whatever.
  • birwin
    birwin over 5 years
    IOC is rendered useless with your suggestion
  • Neo
    Neo over 5 years
    @Luke Sorry, I should've been clearer in that I was referring to the code in the answer referencing ASP.NET MVC. The Web API configuration will still be needed.
  • letie
    letie almost 5 years
    This was it for me! Thanks!