gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:33:46 24 4
gpt4 key购买 nike

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

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

此外,我的 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;
}
}

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

最佳答案

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

  • IAuthorizationService
  • IUserService

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

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

关于.net - Azure 和 Ninject(没有为此对象定义无参数构造函数。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12012735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com