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);   
    }
}
shA.t
  • 16,580
  • 5
  • 54
  • 111
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 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. – Kris Vandermotten Jun 03 '14 at 12:12
  • @KrisVandermotten: `ApiController` does have a protected constructor, but the inheriting class doesn't seem to know that - http://msdn.microsoft.com/en-us/library/system.web.http.apicontroller.apicontroller(v=vs.118).aspx – Dominic Zukiewicz Jun 03 '14 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.) – Jeppe Stig Nielsen Jun 03 '14 at 12:30
  • @JeppeStigNielsen: Absolutely agree, but oddly the solution works for the OP, even though it should do it by default. – Dominic Zukiewicz Jun 03 '14 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()`. – Jeppe Stig Nielsen Jun 03 '14 at 12:37

5 Answers5

15

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?

Pharylon
  • 9,796
  • 3
  • 35
  • 59
8

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.

user2444499
  • 757
  • 8
  • 14
  • I had an Exception occurring in my parameter-less constructor – AJ AJ Feb 15 '22 at 07:59
  • @ AJ AJ No, parameter-full constructor. see https://stackoverflow.com/questions/15908019/simple-injector-unable-to-inject-dependencies-in-web-api-controllers. 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. – qqtf Apr 19 '23 at 13:22
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
}
Dominic Zukiewicz
  • 8,258
  • 8
  • 43
  • 61
1

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").

rsmith
  • 83
  • 6
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();
Joel Wiklund
  • 1,697
  • 2
  • 18
  • 24