无法在asp.net mvc4 Web应用程序中使ninject工作

时间:2013-10-22 17:52:23

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

我正在学习如何在我的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是Nothing。你能告诉我发生了什么吗?

由于

2 个答案:

答案 0 :(得分:3)

根据此页面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。记下命名空间(我使用&#34; VbMvc&#34;我的项目名称)。我没有修改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

答案 1 :(得分:2)

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

如果您想调试{{1}},请按照说明here