我有一个使用 .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