更新
启动Bounty,我添加的是一个复制Ninject错误的Visual Studio 2010解决方案,因为这似乎是一个非常局部化的问题。
当我执行以下操作时:
启动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解决方案,但所有外部文件存储站点都在我的工作场所被阻止,因此如果有人在此期间有任何想法,我会将其链接起来....
我的解决方案是同时手动连接解析器,但我不应该这样做。
答案 0 :(得分:2)
您需要安装Ninject.Mvc3软件包,该软件包是定义和设置依赖项解析程序的软件包。我可以在VS2010解决方案中看到你上传的Ninject.Web.Mvc.dll没有被引用。安装该软件包也将安装Ninject和Ninject.Web.Common,但不是相反。
安装并引用该软件包(Ninject.Mvc3)后,使用MVC 4作为here in this other question可能会遇到同样的问题。确保web.config没有缺少System.Web.Mvc程序集的重定向,从3.0.0.0版到4.0.0.0版。以下行将任何版本的System.Web.Mvc重定向到版本4.0.0.0:
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
希望它有所帮助!
答案 1 :(得分:0)
您需要在CreateKernel方法中设置解析器。
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
创建内核方法:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
DependencyResolver.SetResolver(new NinjectServiceLocator(kernel));
return kernel;
}
注册您的服务:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ITheService>().To<TheService>();
//add other services, etc
}
您可以从this nuget获取Ninject依赖项解析器。
希望这会有所帮助。