UPDATENinject.Web.Common和Ninject.Mvc沒有設置DependencyResolver使用NinjectDependencyResolver
開始賞金,我加入了一個Visual Studio 2010中的解決方案複製Ninject錯誤,因爲這似乎是一個相當本地化的問題。
當我做到以下幾點:
啓動Visual Studio 2010(是2010 :-()
文件>新建項目>空MVC 4應用
管理軟件包>安裝包Ninject.Web.Common(然後安裝Ninject和Ninject.Mvc)
然後做一個HomeController的
代碼如下:
public class HomeController : Controller
{
public HomeController(IKernel kernel)
{
// Should have been injected
}
}
我得到這個:
Server Error in '/' Application.
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.
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, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +6
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a controller of type 'NinjectTest.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +179
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +80
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +74
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +197
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +49
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
當我檢查看看DependencyResolver
是什麼時,我看到這個:
當我做同樣的用VS 2013 Express實例,並創建一個新的MVC 4個空的Web應用程序我看到DependencyResolver
是Ninject之一。
我有一個可運行的VS 2010解決方案,但所有外部文件存儲網站都在我的工作受阻,所以我將其鏈接後,如果有人在此期間的任何想法....
我的解決方案同時手動連接解析器,但我不應該這樣做。
請不要製作您的例外的打印屏幕,但包括完整的例外細節作爲文本。這樣可以更輕鬆地閱讀並允許您的問題被Google編入索引。這樣其他人可以從你的問題中受益。 – Steven
好點我會更新問題,謝謝。 – SimonGates