2017-11-17 14 views
8

Ich entwickle ein MVC5-Projekt auf Visual Studio 2017 Version 15.4. Ich bekomme hier ein unerwartetes Ergebnis, was ich vorher noch nie erlebt habe. Ich habe Ninject.MVC5 Paket von nuget installiert. Es wird schön installiert und gibt keine Fehler oder Warnungen. Aber Problem ist, dass es keine NinjectWebCommon.cs Datei in App_Start Ordner erzeugt. Gibt es einen Grund?Ninject.MVC5 erzeugt kein NinjectWebCommon.Cs

+2

diese https://stackoverflow.com/a/47002329/1236044 helfen sollte – jbl

Antwort

3

Nach viel suchen und Tests habe ich die exakte Lösung bekommt , wo ich Fehler sah, während das System versuchte, mehrere Instanzen gleichzeitig mit der vorherigen Antwort zu erstellen. Hier musste ich NinjectWebCommon Klasse nur ohne Erben erstellen.

public static class NinjectWebCommon 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     var kernel = new StandardKernel(); 
     RegisterServices(kernel); 
     return kernel; 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Load(new INinjectModule[] 
     { 
      new Module() 
     }); 
    } 
} 

Aber hier ist ein Problem mit parametrisierten Konstruktor. Um dieses Problem zu vermeiden, habe ich eine Methode hinzugefügt, um Concrete Instance zu erstellen. So, hier ist der aktualisierte Code ..

public static class NinjectWebCommon 
{ 
    private static readonly Bootstrapper bootstrapper = new Bootstrapper(); 

    /// <summary> 
    /// Starts the application 
    /// </summary> 
    public static void Start() 
    { 
     DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule)); 
     DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule)); 
     bootstrapper.Initialize(CreateKernel); 
    } 

    /// <summary> 
    /// Stops the application. 
    /// </summary> 
    public static void Stop() 
    { 
     bootstrapper.ShutDown(); 
    } 

    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    /// <summary> 
    /// Creates the kernel that will manage your application. 
    /// </summary> 
    /// <returns>The created kernel.</returns> 
    private static IKernel CreateKernel() 
    { 
     return Container; 
    } 

    public static T GetConcreteInstance<T>() 
    { 
     object instance = Container.TryGet<T>(); 
     if (instance != null) 
      return (T)instance; 
     throw new InvalidOperationException(string.Format("Unable to create an instance of {0}", typeof(T).FullName)); 
    } 

    public static IKernel _container; 
    private static IKernel Container 
    { 
     get 
     { 
      if (_container == null) 
      { 
       _container = new StandardKernel(); 
       _container.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
       _container.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 

       RegisterServices(_container); 
      } 
      return _container; 
     } 
    } 

    /// <summary> 
    /// Load your modules or register your services here! 
    /// </summary> 
    /// <param name="kernel">The kernel.</param> 
    private static void RegisterServices(IKernel kernel) 
    { 
     kernel.Load(new INinjectModule[] 
     { 
      new Module() 
     }); 
    } 
} 
7

Es sieht so aus, als ob das neueste Ninject.Web.Common.WebHost 3.3.0 NuGet-Paket nicht mehr die NinjectWebCommon.cs enthält. Ältere Versionen wie 3.2.0 enthalten diese Datei.

Ninject.Web.Common.WebHost 3.3.0 bietet eine NinjectHttpApplication-Klasse, die Sie anstelle von NinjectWebCommon.cs verwenden können. Die Wiki-Dokumentation für Ninject nicht aktualisiert worden scheint, aber es sieht aus wie mit der NinjectHttpApplication einem dokumentiert Ansatz

mat Kommentar sehen - Web API2 NinjectWebCommon.cs do not appear

+2

Version 3.3.0 ist auch nicht parametrisiert Controller Konstruktor Abhängigkeit zu lösen. –

Verwandte Themen