Ninject in ASP.NET MVC4

前端 未结 8 1955
终归单人心
终归单人心 2021-02-07 03:19

So after much screwing around I finally got Ninject wired in and compiling in my MVC4 application. The problem I was running into is the IDependencyScope interface no longer ex

相关标签:
8条回答
  • 2021-02-07 03:52

    I have also had this problem when I used nuget to install Ninject.MVC4 in a project referenced by my actual MVC website project.

    The trouble is that the NinjectWebCommon.cs file automatically installed in the App_Start directory of the referenced project conflicts with the (actual, useful) one installed in my website project. Removing the NinjectWebCommon.cs file from the referenced project resolves the error.

    0 讨论(0)
  • 2021-02-07 03:53

    Just delete NinjectWebCommon.cs file from your project (it is in App_Start folder). and everything should be working.

    Source: http://mlindev.blogspot.com.au/2012/09/how-to-implement-dependency-injection.html

    0 讨论(0)
  • 2021-02-07 03:58

    When you will install latest Ninject.MVC3 from NuGet package we find following code on top of the NinjectWebCommon.cs file:

    [assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Start")]
    
    [assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication1.App_Start.NinjectWebCommon), "Stop")]
    

    in this case we dont need to register ninject explicitly in global.asax

    I found a good content on using Ninject with MVC 4 here

    0 讨论(0)
  • 2021-02-07 03:58

    I have come across the same issue not quite sure what has fixed after below changes

    added Ninject.MVC4 to project

    deleted NinjectWebCommon.cs (the generated file, as the integration already exists in global.ascx.cs file)

    0 讨论(0)
  • 2021-02-07 04:00

    I tend to keep my Ninject bootstrapping in a separate project. In order to use the .InRequestScope() extension method of IBindingInSyntax<T>, I had added via Nuget the Ninject.Web.Common library. Alas, this library includes the app_start bootstrapper, resulting in duplicate NinjectWebCommon classes and attachment via WebActivator (1 in said project and 1 in the MVC project itself).

    I deleted the duplicate App_Start folder from my bootstrap project, and this solved it.

    0 讨论(0)
  • 2021-02-07 04:02

    Ok after beating my head against the wall for far too long I figured out what was going on. The default project type for MVC4 running on .NET 4.5 had a reference to the original RC version of System.Web.Http instead of the updated version.

    Namespaces were missing, objects didn't exist, life was not good.

    Steps for resolution:

    1. Remove your reference to System.Web.Http in your MVC4 project
    2. Add Reference -> System.Web.Http
    3. Delete all work arounds you put in to get the old garbage version of System.Web.Http to work
    4. Reapply standard process to wire in Ninject.

      HOWEVER, the error of:

      Exception Details: Ninject.ActivationException: Error activating IntPtr No matching bindings are available, and the type is not self-bindable. Activation path: 3) Injection of dependency IntPtr into parameter method of constructor of type Func{IKernel} 2) Injection of dependency Func{IKernel} into parameter lazyKernel of constructor of type HttpApplicationInitializationHttpModule 1) Request for IHttpModule

      Suggestions: 1) Ensure that you have defined a binding for IntPtr. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.

    Update This was solved by updating MVC from MVC4 Beta to MVC4 RC.

    0 讨论(0)
提交回复
热议问题