【问题标题】:Azure and Ninject (No parameterless constructor defined for this object.)Azure 和 Ninject(没有为此对象定义无参数构造函数。)
【发布时间】:2012-08-17 20:27:18
【问题描述】:

我有一个使用 .NET Framework 4、MVC 4、Ninject 3 的 Web 应用程序。最初我使用的是 .NET Framework 4.5,但我将“Target Framework”更改为 4.0 并修复了它导致的所有错误。现在该应用程序在我的本地机器上完美运行。问题是,当我将它部署到 Azure 时,我在这篇文章的底部收到了服务器错误。

我以前见过这个错误。当我第一次开始使用 Ninject 时,当我在 Global.asax 中没有正确绑定时会出现此错误(相关代码如下)。

另外,我的 Ninject.dll 引用“复制本地”属性设置为“真”。

我做错了什么?为什么当我尝试在 Azure 中查看 Web 应用程序而不是在我的本地机器上时出现此错误?

感谢您的帮助,

亚伦

HomeController.cs

public class HomeController : Controller
{
    IAuthorizationService _authorizationService;
    IUserService _userService;

    public HomeController(IAuthorizationService authorizationService, IUserService userService)
    {
        _authorizationService = authorizationService;
        _userService = userService;
    }
}

Global.asax

protected void Application_Start()
{
    SetupDependencyInjection();
}

private void SetupDependencyInjection()
{
    //create Ninject DI Kernel
    IKernel kernel = new StandardKernel();

    //register services with Ninject DI container
    RegisterServices(kernel);

    //tell asp.net mvc to use our Ninject DI Container
    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

private void RegisterServices(IKernel kernel)
{
    kernel.Bind<IAccountService>().To<AccountService>().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["AccountManagerEntities"].ConnectionString);
}

服务器错误

No parameterless constructor defined for this object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.MissingMethodException: No parameterless constructor defined for this object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[MissingMethodException: No parameterless constructor defined for this object.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) +117
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) +247
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +106
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +84

[InvalidOperationException: An error occurred when trying to create a controller of type 'AccountManager.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +85
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +280
   System.Web.Mvc.<>c__DisplayClass6.<BeginProcessRequest>b__2() +66
   System.Web.Mvc.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a() +19
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Func`1 func) +161
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +405
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

【问题讨论】:

  • 您在 Global.asax 中的什么时候真正调用了您的初始化方法。你是从 Application_Start 开始做的吗??

标签: .net asp.net-mvc azure ninject


【解决方案1】:

这个问题实际上是一个 Ninject 配置问题。在您的 RegisterServices 方法中,您正在注册 1 个接口:IAccountService。您在这里缺少的是您尝试在 HomeController 中注入的接口的注册:

  • IAuthorizationService
  • IUserService

由于 Ninject 不知道这些接口的任何实现,它不能使用您在 HomeController 类中指定的构造函数。正在寻找默认的无参构造函数,但是这个构造函数不存在。

解决方案很简单,您需要注册IAuthorizationServiceIUserService 才能正常工作。

【讨论】:

    【解决方案2】:

    Sandrino Di Mattia,你是对的,我需要注册其他接口。实际上,我确实让他们注册了。为了在我的示例代码中直截了当,我删除了相关的部分。 :p

    我修复它的方法只是使用 NuGet 卸载 Ninject,然后使用 NuGet 重新安装它。我在互联网上读过一篇文章,说我必须使用 Entity Framework 执行此操作,才能在从 4.5 重新定位后使用 .NET 4.0 正确安装。因此,当这解决了我的 EF 问题时,我尝试使用 Ninject 并成功了。

    因此,这一切的意义在于,从 .NET 4.5 重新定位到 .NET 4.0,卸载 NuGet 包,然后重新安装它们。

    亚伦

    【讨论】:

    • 感谢您的提示。我最近从 4.0 重新定位到 4.5,但不明白为什么构造函数注入不再起作用。
    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2016-04-25
    • 2018-02-11
    相关资源
    最近更新 更多