Ich teste Ninject, aber nach dem How-To finde ich es unmöglich, es zum Laufen zu bringen. Die Informationen im Web sind so chaotisch und sogar widersprüchlich. Ich entwickle eine Website in MVC 4 auf Visual Studio 2012 und ich habe Ninject mit Nuget installiert.Ninject "Kein parameterloser Konstruktor für dieses Objekt definiert."
So bekomme ich einen Fehler: "Kein Parameterloser Konstruktor für dieses Objekt definiert.". Sobald ich meinen Controller betrete.
habe ich die notwendigen Schritte:
- Nuget Installation
- in NinjectWebCommon.cs, ich habe meine Schnittstelle im RegisterServices Methode registrieren.
In meinem Homecontroller ich mein Objekt wie folgt:
public ISurvey _survey { get; set; } [Inject] public HomeController(ISurvey s) { _survey = s; }
Und bekomme ich folgende Fehlermeldung:
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. 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, 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) +110 [InvalidOperationException: An error occurred when trying to create a controller of type 'Surveys.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.GetControllerInstance(RequestContext requestContext, Type controllerType) +438 System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +226 System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +177 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
So frage ich mich, was habe ich falsch gemacht? 4 Stunden darauf habe ich festgestellt, dass es ein Problem mit meiner web.config-Datei geben kann oder dass es eine fehlende Referenz geben könnte, oder es könnte eine Fabrik fehlen. Im Moment weiß ich nicht einmal, was ich tun soll. Danke für Ihre Hilfe.
Können Sie den Inhalt von NinjectWebCommon.cs hinzufügen? – treze
Können Sie den Inhalt der ISurvey-Implementierung hinzufügen? Übrigens, Sie müssen den Konstruktor nicht mit dem Inject-Attribut dekorieren, aber ich glaube nicht, dass das bei Ihrem Problem einen Unterschied macht. – Hernan