not able to make ninject work in asp.net mvc4 web application

久未见 提交于 2019-12-06 04:24:59
Jasen

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!