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);
}
}
4 个解决方案
#1
4
Add a public parameterless constructor and the problem goes away:
添加一个公共无参数构造函数,问题就解决了:
public class AnalyticController : ApiController
{
public AnalyticController()
{
}
// Variables and methods are the same as before
}
#2
9
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?
这取决于您如何处理DI(依赖注入)。默认情况下,控制器需要无参数构造函数,必须使用Unity或Ninject之类的东西来处理DI。我看到你从ApiController那里继承了,所以如果你使用Ninject,一定要下载Ninject作为Web API。如果你已经知道我刚才告诉你的一切,那么你需要提供更多的信息。你的设置是什么?你用什么来做DI?
#3
3
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.
我得到这个错误是因为在我的参数构造函数中发生了一个异常。它与需要一个无参数的构造函数无关,尽管有错误消息。
#4
0
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).
不知道这会不会对其他人有帮助。我在本地(dev)框中构建并运行我的应用程序。然后在部署到产品之前,我运行应用程序指向现有的产品web服务。这通常是可行的,但这一次,它在“dev”中工作,但是给了我这个“确保控制器在生产中有一个无参数的公共构造函数“error message”(实际上,不是在生产中,而是针对生产web服务)。
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").
这些解决方案中没有一个看起来像我的代码的问题,当我将代码部署到生产环境中时,我没有在生产环境中得到这个错误。我猜想这可能与开发框和产品之间的NuGet包的不同版本有关。我将进行调查,但我只是想提供这一点,以防其他人有类似的“针对dev而不是prod”的情况。它可能是一个环境问题(取决于您所说的“在prod中不工作”)。
#1
4
Add a public parameterless constructor and the problem goes away:
添加一个公共无参数构造函数,问题就解决了:
public class AnalyticController : ApiController
{
public AnalyticController()
{
}
// Variables and methods are the same as before
}
#2
9
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?
这取决于您如何处理DI(依赖注入)。默认情况下,控制器需要无参数构造函数,必须使用Unity或Ninject之类的东西来处理DI。我看到你从ApiController那里继承了,所以如果你使用Ninject,一定要下载Ninject作为Web API。如果你已经知道我刚才告诉你的一切,那么你需要提供更多的信息。你的设置是什么?你用什么来做DI?
#3
3
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.
我得到这个错误是因为在我的参数构造函数中发生了一个异常。它与需要一个无参数的构造函数无关,尽管有错误消息。
#4
0
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).
不知道这会不会对其他人有帮助。我在本地(dev)框中构建并运行我的应用程序。然后在部署到产品之前,我运行应用程序指向现有的产品web服务。这通常是可行的,但这一次,它在“dev”中工作,但是给了我这个“确保控制器在生产中有一个无参数的公共构造函数“error message”(实际上,不是在生产中,而是针对生产web服务)。
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").
这些解决方案中没有一个看起来像我的代码的问题,当我将代码部署到生产环境中时,我没有在生产环境中得到这个错误。我猜想这可能与开发框和产品之间的NuGet包的不同版本有关。我将进行调查,但我只是想提供这一点,以防其他人有类似的“针对dev而不是prod”的情况。它可能是一个环境问题(取决于您所说的“在prod中不工作”)。