问题
I am learning how to use Ninject in my asp.net MVC 4 web application. I am not sure I have got the concepts right. This is what I did.
1) Installed Ninject.MVC3 using Manage NuGet
2) Added the following code in NinjectWebCommon.cs(this file was added automatically by Nuget in App_start folder)
private static void RegisterServices(IKernel kernel)
{
**kernel.Bind<IProductRepository>.To<ProductRepository>;**
}
3) Controller Code
Public Class ProductController
Inherits System.Web.Mvc.Controller
Private _rProduct As IProductRepository
'
' GET: /Product
Sub ProductController(ProductRepository As IProductRepository)
_rProduct = ProductRepository
End Sub
Function ProductList() As ActionResult
Return View(_rProduct.GetProducts())
End Function
End Class
4) IProductRepository code
Public Interface IProductRepository
Function GetProducts() As IQueryable(Of Product)
End Interface
5) ProductRepository code
Public Class ProductRepository
Implements IProductRepository
Public Function GetProducts() As IQueryable(Of Product) Implements IProductRepository.GetProducts
Return (New List(Of Product)() From {
New Product() With {.Name = "Football", .Price = 25},
New Product() With {.Name = "Surf board", .Price = 179},
New Product() With {.Name = "Running shoes", .Price = 95}
}.AsQueryable())
End Function
End Class
When I debug, the control does not go to the breakpoint in RegistryServices() in NinjectWebCommon.cs instead comes to the ProductList() in the ProductController. At this point _rProduct is Nothing. Can you please explain me what is happening?
Thanks
回答1:
According to this page https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application you have some extra steps to perform because you are using VB.Net.
NOTE: If you're using VB.NET to develop an MVC3 application then there are few additional steps to make everything work as expected:
- In App_Start: rename NinjectMVC3.cs to NinjectMVC3.vb
- Replace contents of NinjectMVC3.vb with contents provided in this gist: https://gist.github.com/923618
Edit: Here's a NinjectWebCommon.vb that I got working. Make note of the Namespace (I'm using "VbMvc" for my project name). I did not modify Global.asax.vb and breakpoints are hit in RegisterServices.
Imports Microsoft.Web.Infrastructure.DynamicModuleHelper
Imports Ninject.Web.Common
Imports Ninject.Web
Imports Ninject
Imports Ninject.Web.Mvc
<Assembly: WebActivator.PreApplicationStartMethod(GetType(VbMvc.App_Start.NinjectWebCommon), "StartNinject")>
<Assembly: WebActivator.ApplicationShutdownMethodAttribute(GetType(VbMvc.App_Start.NinjectWebCommon), "StopNinject")>
Namespace VbMvc.App_Start
Public Module NinjectWebCommon
Private ReadOnly bootstrapper As New Bootstrapper()
''' <summary>
''' Starts the application
''' </summary>
Public Sub StartNinject()
DynamicModuleUtility.RegisterModule(GetType(NinjectHttpModule))
DynamicModuleUtility.RegisterModule(GetType(OnePerRequestHttpModule))
bootstrapper.Initialize(AddressOf CreateKernel)
End Sub
''' <summary>
''' Stops the application.
''' </summary>
Public Sub StopNinject()
bootstrapper.ShutDown()
End Sub
''' <summary>
''' Creates the kernel that will manage your application.
''' </summary>
''' <returns>The created kernel.</returns>
Private Function CreateKernel() As IKernel
Dim kernel = New StandardKernel()
kernel.Bind(Of Func(Of IKernel))().ToMethod(Function(ctx) Function() New Bootstrapper().Kernel)
kernel.Bind(Of IHttpModule)().To(Of HttpApplicationInitializationHttpModule)()
RegisterServices(kernel)
Return kernel
End Function
''' <summary>
''' Load your modules or register your services here!
''' </summary>
''' <param name="kernel">The kernel.</param>
Private Sub RegisterServices(ByVal kernel As IKernel)
''kernel.Load(New Bindings.ServiceBindings(), New Bindings.RepositoryBindings(), New Bindings.PresentationBindings(), New Bindings.CrossCuttingBindings())
End Sub
End Module
End Namespace
回答2:
You can see in your Global.asax
that RegisterServices()
is invoked by the Application_Start()
method that is invoked once upon application start (or recycle) and not for every http request.
If you want to debug your Application_Start()
follow the instructions here.
来源:https://stackoverflow.com/questions/19524946/not-able-to-make-ninject-work-in-asp-net-mvc4-web-application