我有一个使用.NET Framework 4,MVC 4,Ninject 3的Web应用程序。最初我使用的是.NET Framework 4.5,但我将“Target Framework”更改为4.0并修复了它导致的所有错误。现在应用程序在我的本地盒子上完美运行。问题是当我将它部署到Azure时,我在这篇文章的底部得到了服务器错误。
我以前见过这个错误。当我第一次开始使用Ninject时,如果我在Global.asax(下面的相关代码)中没有正确绑定,我会收到此错误。
另外,我的Ninject.dll引用“Copy Local”属性设置为“True”。
我做错了什么?当我尝试在Azure中查看Web应用程序而不是在本地框中查看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
答案 0 :(得分:4)
此问题实际上是Ninject配置问题。在RegisterServices
方法中,您正在注册1个界面:IAccountService
。你在这里缺少的是你试图在HomeController中注入的接口的注册:
IAuthorizationService
IUserService
由于Ninject不知道这些接口的任何实现,因此它不能使用您在HomeController
类上指定的构造函数。它正在寻找默认的无参数构造函数,但这个构造函数不存在。
解决方案很简单,您需要注册IAuthorizationService
和IUserService
才能使其正常运行。
答案 1 :(得分:4)
Sandrino Di Mattia,你是对的,我需要注册其他接口。实际上,我确实让他们注册了。为了在我的示例代码中找到重点,我删除了相关的部分。 :P
我修复它的方法只是用NuGet卸载Ninject然后用NuGet重新安装它。我曾在互联网上阅读过一篇文章,说我必须使用Entity Framework执行此操作,以便在从4.5重新定位后可以使用.NET 4.0正确安装。所以,当这解决了我的EF问题时,我尝试使用Ninject并且它有效。
所以,所有这些的道德是从.NET 4.5重新定位到.NET 4.0,卸载NuGet包然后重新安装它们。
亚伦