4

I know the error "No parameterless constructor defined for this object" has been asked about a million times. My situation is different

I have a working app. Many many controllers and one area with lots of controllers. I just added a new area. I added a controller and then a link to that controller. NOW I get the "No parameterless constructor defined for this object" error

I have seen and conquered this problem before but it really only happens like once every 5 months. And everytime I have totally forgotten ( repressed ) the answer.

Please help

Raif

Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
Raif
  • 8,641
  • 13
  • 45
  • 56
  • 1
    Sounds as if the controller is not being created by structuremap? – Lucero Dec 07 '11 at 22:01
  • can you please paste your code example of where you have the constructors and where you are using the objects the error sounds like you created some method overloads and are calling the incorrect constructor – MethodMan Dec 07 '11 at 22:05
  • This controller is almost exactly like all the other controllers in this application. There is only one constructor. It has several services being injected into it and the debugger never gets into action – Raif Dec 07 '11 at 23:19

2 Answers2

9

Ok, so it seems that there are several reasons one might get this error. Not surprisingly none of them have @#$% all to do with not having a parameterless constructor. The two that I'm aware of are

1) if you are using an area and you, say, move a controller from one namespace to the new one and don't update the namespace to reflect the area you will get this error.

2) and this is my situation now, if you are injecting something into the constructor of the controller and the item you are injecting has a problem with it ( there are no instantiations, or it's not registered in your IOC registration or some other runtime error ) you will get this error.

If people can think of others they should list them here because I think there are several more causes for the error. R

Raif
  • 8,641
  • 13
  • 45
  • 56
  • 3
    If you debug the project in Visual Studio and check the Output panel, you will see a more detailed message that tells you exactly which object StructureMap was unable to construct. I have seen the error occur because of incorrect access modifiers - e.g. if your interface is public but your concrete class isn't, StructureMap may be unable to construct an instance of the concrete class. – Richard Aug 24 '12 at 19:20
  • Also need to be sure that the concrete class inherits from the interface. – d456 Apr 11 '13 at 16:58
  • Thanks for this. I am just learning SM and I couldn't get past this error. Thanks to your tip, I realized my sample class didn't have a "public" accessor. Stupid mistake on my part, but the error message did not help at all. – camainc Feb 02 '16 at 22:10
  • I faced the same issue and the cause is " if you are injecting something into the constructor of the controller and the item you are injecting has a problem with it". Actually, I got the typo in DAL class name+(controller inject Iservice, service inject IDal), so the SM can not scan and get the correct instance. And I only can find out the root cause when debugging in vs2015. in vs2010 sp1, the error message is quite common "No parameterless constructor defined for this object." – Chinh Phan Sep 09 '16 at 06:52
0

While I realize this doesn't truly answer your question, your answer helped me in my troubleshooting endeavors.

I just recently came across this same issue while using MVC Portable Areas from the MVC Contrib project. I found that any dll dependancies the portable areas had must also be included when scanning for assemblies during IOC registration, something along the lines of this:

ObjectFactory.Initialize(x => x.Scan(y =>
    {
        y.Assembly("PortableAreaAssemblyName");
    }));
ObjectFactory.Configure(x =>
    {
        x.For<IClassInterfaceUsedByControllerConstructor>().Use<IntendedClassInstance>();
    });
akousmata
  • 1,005
  • 14
  • 34