3

我有一个使用 .NET Framework 4、MVC 4、Ninject 3 的 Web 应用程序。最初我使用的是 .NET Framework 4.5,但我将“目标框架”更改为 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;
    }
}

全球.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
4

2 回答 2

4

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

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

所以,所有这一切的寓意是当从 .NET 4.5 重新定位到 .NET 4.0 时,卸载你的 NuGet 包,然后重新安装它们。

亚伦

于 2012-08-24T22:47:32.380 回答
4

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

  • IAuthorizationService
  • IUserService

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

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

于 2012-08-17T21:58:45.110 回答