2013-10-22 59 views
3

我正在學習如何在我的asp.net MVC 4 web應用程序中使用Ninject。我不確定我是否有正確的概念。這就是我所做的。無法在asp.net mvc4 web應用程序中使ninject工作

1)使用管理的NuGet

2)安裝Ninject.MVC3添加在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 

當我調試,控制不轉到 RegistryServices斷點()在NinjectWebCommon.cs,而不是來自於ProductController的的產品列表()。此時_rProduct是Nothing。你能解釋我發生了什麼嗎?

感謝

回答

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。記下命名空間(我正在使用「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 
+0

我做了你提到的改變,但得到了相同的結果。在調試時,它會觸發控制器 - > ProductList()中的中斷點,並且_rProducts仍然沒有任何結果! – user2721870

+0

您是否記得恢復您的'RegisterServices'增加? – Jasen

+0

我做了:) Private Sub RegisterServices(ByVal kernel As IKernel) kernel.Bind(Of IProductRepository).To(Of ProductRepository)() End Sub – user2721870

2

你可以看到你的Global.asaxRegisterServices()由被依申請啓動(或再循環)和爲每一個HTTP請求調用一次Application_Start()方法調用。

如果您想調試您的Application_Start()請按照說明here

相關問題