【问题标题】:not able to make ninject work in asp.net mvc4 web application无法使 ninject 在 asp.net mvc4 Web 应用程序中工作
【发布时间】:2013-10-22 17:52:23
【问题描述】:

我正在学习如何在我的 asp.net MVC 4 Web 应用程序中使用 Ninject。我不确定我的概念是否正确。这就是我所做的。

1) 使用 Manage NuGet 安装 Ninject.MVC3

2) 在 NinjectWebCommon.cs 中添加如下代码(该文件由 Nuget 自动添加到 App_start 文件夹中)

  private static void RegisterServices(IKernel kernel)
    {
        **kernel.Bind<IProductRepository>.To<ProductRepository>;**
    }  

3) 控制器代码

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 代码

Public Interface IProductRepository
Function GetProducts() As IQueryable(Of Product)
End Interface

5) ProductRepository 代码

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

当我调试时,控件没有转到断点 NinjectWebCommon.cs 中的 RegistryServices() 转而访问 ProductController 中的 ProductList()。此时 _rProduct 什么都没有。你能解释一下发生了什么吗?

谢谢

【问题讨论】:

    标签: asp.net vb.net asp.net-mvc-4 ninject


    【解决方案1】:

    根据此页面https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application,您需要执行一些额外的步骤,因为您使用的是 VB.Net。

    注意:如果您使用 VB.NET 开发 MVC3 应用程序,则只需几个额外的步骤即可使一切按预期工作:

    • 在 App_Start 中:将 NinjectMVC3.cs 重命名为 NinjectMVC3.vb
    • 将 NinjectMVC3.vb 的内容替换为此要点中提供的内容:https://gist.github.com/923618

    编辑:这是我开始工作的 NinjectWebCommon.vb。记下命名空间(我使用“VbMvc”作为我的项目名称)。我没有修改 Global.asax.vb 并且在 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
    

    【讨论】:

    • 我做了你提到的改变,但得到了相同的结果。在调试时,它在控制器中遇到断点->ProductList() 并且 _rProducts 仍然没有!
    • 你记得恢复你的RegisterServices添加吗?
    • 我做了 :) Private Sub RegisterServices(ByVal kernel As IKernel) kernel.Bind(Of IProductRepository).To(Of ProductRepository)() End Sub
    • 另外,您是否在要点中看到有关版本差异的 cmets?有人为要点发布了修改后的代码。
    • 我也这样做了。我真的很想知道为什么它没有在 RegisterServices 方法中达到我的断点!
    【解决方案2】:

    您可以在您的Global.asax 中看到RegisterServices()Application_Start() 方法调用,该方法在应用程序启动(或回收)时调用一次对于每个 http 请求。

    如果您想调试您的Application_Start(),请按照说明here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多