12

I get this error

An error occurred when trying to create a controller of type 'AnalyticController'. Make sure that the controller has a parameterless public constructor.

Code works in test environment but not on the production server.

Any idea what could cause the problem?

This is my controller

public class AnalyticController : ApiController
{
    private AnalyticBLL analyticBLL = new AnalyticBLL();

    // POST api/status
    public void Post(AnalyticDTO analyticDTO)
    {
        if (!analyticBLL.Register(analyticDTO))
            helpers.BusinessLayer.CreateResponseException(analyticBLL.Errors);   
    }
}
5
  • 4
    Your controller (as posted here) does have a parameterless public constructor. Your real controller probably has another constructor, not posted here, and as a result the default parameterless constructor is not generated. Commented Jun 3, 2014 at 12:12
  • @KrisVandermotten: ApiController does have a protected constructor, but the inheriting class doesn't seem to know that - msdn.microsoft.com/en-us/library/… Commented Jun 3, 2014 at 12:16
  • 1
    @DominicZukiewicz The inheriting class knows about the constructor in the base class. If it couldn't see any constructor in the base class, it wouldn't even be possible to inherit from that base class (since the instance constructor(s) must chain an (existing and visible) constructor in the base class). (Disregarding the pathological case where all instance constructors chain constructors in this class with :this(...) in a cyclic way.) Commented Jun 3, 2014 at 12:30
  • @JeppeStigNielsen: Absolutely agree, but oddly the solution works for the OP, even though it should do it by default. Commented Jun 3, 2014 at 12:31
  • @DominicZukiewicz Not sure what you mean. As Kris Vandermotten said, the Original-Poster must have some explicit instance constructor which suppresses the generation of an "automatic" public default constructor. If you omit the chain-other-constructor syntax in an instance constructor, that is equivalent to : base(). Commented Jun 3, 2014 at 12:37

5 Answers 5

16

That depends on how you're handling DI (dependency injection). By default, controllers need paramaterless constructor, and you have to use something like Unity or Ninject to handle DI. I see you're inhereiting from ApiController, so if you're using Ninject make sure you download Ninject for Web API. If you already know everything I just told you, then you need to give more information. What's your setup? What are you using for DI?

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

Comments

9

I was getting this error because I had an Exception occurring in my parameter-ful constructor. It had nothing to do with requiring a parameter-less constructor despite the error message.

3 Comments

I had an Exception occurring in my parameter-less constructor
@ AJ AJ No, parameter-full constructor. see stackoverflow.com/questions/15908019/…. The error message indicates that the framework is looking for a parameterless constructor, as a fall back option, because it fails to DI resolve one or more parameters of the constructor that is defined.
Exactly -- yes, this is maddening that a missing injection would end up with this misleading an exception, or no further inner exception revealing the true cause. In my case, using Ninject & ASP.net (still in framework not core).
5

Add a public parameterless constructor and the problem goes away:

public class AnalyticController : ApiController
{
   public AnalyticController()
   {
   }

   // Variables and methods are the same as before
}

Comments

2

Don't know if this will help anybody else. I build & then run my app against web services all on my local (dev) box. Then before I deploy to production, I run my app pointing to the existing production web services. This usually works, but this time, it worked "in dev" but gave me this "Make sure that the controller has a parameterless public constructor" error message "in production" (well, not really in production but against the production web services).

None of these solutions seemed like the problem for my code and when I deployed my code to production, I did not get this error when it is all in production. My guess is that it may have to do with different versions of the NuGet packages between my dev box & production. I'll investigate, I but just wanted to offer this up in case others have a similar "works against dev but not prod" situation. It may be an environment issue (depending on what you mean by does not work "in prod").

Comments

0

For me I had done double registration of a dependency like so:

kernel.Bind<IMemoryCacheService>().To<MemoryCacheService>().InSingletonScope();
kernel.Bind<IMemoryCacheService>().To<MemoryCacheService>().InSingletonScope();

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.