Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Azure和Ninject(没有为此对象定义无参数构造函数。)_.net_Asp.net Mvc_Azure_Ninject - Fatal编程技术网

.net Azure和Ninject(没有为此对象定义无参数构造函数。)

.net Azure和Ninject(没有为此对象定义无参数构造函数。),.net,asp.net-mvc,azure,ninject,.net,Asp.net Mvc,Azure,Ninject,我有一个使用.NETFramework4、MVC4和Ninject 3的web应用程序。最初我使用的是.NETFramework4.5,但我将“目标框架”更改为4.0,并修复了它导致的所有错误。现在,该应用程序在我的本地设备上完美运行。问题是,当我将其部署到Azure时,我在本文底部看到了服务器错误 我以前见过这个错误。当我第一次开始使用Ninject时,当我在Global.asax(下面的相关代码)中没有正确绑定时,我会遇到这个错误 此外,我的Ninject.dll引用“Copy Local”

我有一个使用.NETFramework4、MVC4和Ninject 3的web应用程序。最初我使用的是.NETFramework4.5,但我将“目标框架”更改为4.0,并修复了它导致的所有错误。现在,该应用程序在我的本地设备上完美运行。问题是,当我将其部署到Azure时,我在本文底部看到了服务器错误

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

此外,我的Ninject.dll引用“Copy Local”属性设置为“True”

我做错了什么?为什么当我尝试在Azure中查看web应用程序而不是在本地框中查看时会出现此错误

谢谢你的帮助

亚伦

HomeController.cs

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

    public HomeController(IAuthorizationService authorizationService, IUserService userService)
    {
        _authorizationService = authorizationService;
        _userService = userService;
    }
}
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

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

    public HomeController(IAuthorizationService authorizationService, IUserService userService)
    {
        _authorizationService = authorizationService;
        _userService = userService;
    }
}
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
受保护的无效应用程序\u Start()
{
SetupDependencyInjection();
}
私有void SetupDependencyInjection()
{
//创建Ninject DI内核
IKernel kernel=新的标准内核();
//使用Ninject DI容器注册服务
注册服务(内核);
//告诉asp.net mvc使用我们的Ninject DI容器
SetResolver(新的NinjectDependencyResolver(内核));
}
私有无效注册服务(IKernel内核)
{
kernel.Bind().To().WithConstructorArgument(“connectionString”,ConfigurationManager.connectionString[“AccountManagerEntities”].connectionString);
}
服务器错误

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

    public HomeController(IAuthorizationService authorizationService, IUserService userService)
    {
        _authorizationService = authorizationService;
        _userService = userService;
    }
}
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
没有为此对象定义无参数构造函数。
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。
异常详细信息:System.MissingMethodException:未为此对象定义无参数构造函数。
源错误:
在执行当前web请求期间生成了未经处理的异常。有关异常的起源和位置的信息可以使用下面的异常堆栈跟踪来识别。
堆栈跟踪:
[MissingMethodException:没有为此对象定义无参数构造函数。]
System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)+0
System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache)+117
System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipVisibilityChecks、布尔skipCheckThis、布尔fillCache)+247
System.Activator.CreateInstance(类型,布尔非公共)+106
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext-RequestContext,类型controllerType)+84
[InvalidOperationException:尝试创建“AccountManager.Controllers.HomeController”类型的控制器时出错。请确保该控制器具有无参数公共构造函数。]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext-RequestContext,Type controllerType)+247
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext RequestContext,String controllerName)+85
ProcessRequestInit(HttpContextBase httpContext、IController和controller、IControllerFactory和factory)+280
System.Web.Mvc.c__显示类6.b__2()+66
System.Web.Mvc.c__显示类b`1.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步骤,布尔值&同步完成)+375

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

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


解决方案很简单,您需要注册
IAuthorizationService
IUserService
,以使其正常工作。

Sandrino Di Mattia,您是对的,我需要注册其他接口。事实上,我已经登记了。为了切中要害,我在示例代码中删去了相关部分:p

我修复它的方法只是用NuGet卸载Ninject,然后用NuGet重新安装它。我曾在互联网上读到一篇文章,其中说我必须使用Entity Framework来实现这一点,以便在从4.5重新定位后,能够使用.NET 4.0正确安装它。所以,当这解决了我的EF问题时,我用Ninject尝试了它,它成功了

因此,所有这些的寓意是当从.NET 4.5重新定位到.NET 4.0时,卸载您的NuGet软件包,然后重新安装它们


Aaron

在您的Global.asax中,您实际上是在什么时候调用初始化方法的。你是从应用程序开始做的吗?谢谢你的提示。我最近将目标从4.0重定为4.5,不明白为什么构造函数注入不再有效。