Wayne Feltham Wayne Feltham - 1 year ago 83
Vb.net Question

ASP.NET Web Forms + Ninject + Helper Class with iService - how to make Ninject handle iService?

I have an ASP.NET Web Forms project written in VB.NET running on .NET 4.5.2

I've successfully plugged Ninject into my application however I've hit a snag.

I'm using Ninject Property Injection within my web pages (sample below)

Partial Class MyPage
Inherits Page

Public Property _myService As IService

All calls to this work...so this is fine


However, within my application we have a class that we use to wrap calls into our service (see below)

Public Class MyWrapper

Public Property _myService As IService

Public Sub DoWork()
End Sub

End Class

This class is used by web pages...

Dim wrapper As New MyWrapper()


This doesn't work - within the MyWrapper class, Ninject isn't injecting the IService.

I'm guessing that because my class isn't a web page, it's not in the pipeline and so Ninject isn't doing anything with it.

I've read some other posts that suggest amending MyWrapper to inherit from Ninject.Web.PageBase but having tried this it doesn't seem to work.

Has anyone got any suggestions?

MyWrapper has 100+ references within our application so ideally I don't want to have to change every line of code.

Also, the MyWrapper class has some Shared/Static methods - will this cause a problem (I can change this if necessary).

FYI - the above code should be treated a pseudo code - I typed it adhoc rather than copy-pasting - and so it may contain syntax errors.

Answer Source

Finally worked it out.

So, I couldn't get MyWrapper class to use Ninject. My assumption here is that because this is a web forms application (rather than MVC) the pipeline is different and MyWrapper class isn't run through the same pipeline and so Ninject is never aware of it.

Here is what I did to "fix" my problem.

I created a new Base page

Public Class MyBasePage
    Inherits System.Web.UI.Page

    Public Property wrapper As MyWrapper
End Class

I then changed my .aspx.vb page (MyPage) to inherit from the above...

Partial Class MyPage
    Inherits MyBasePage

I previously forgot to mention that I had added MyWrapper to the NinjectWebCommon.vb file...

Private Shared Sub RegisterServices(kernel As IKernel)
    'The MappingModule is my external code where I Bind my interfaces to the concrete classes
    'So for the purpose of this example, the MappingModule resolves IService
    Dim mappingModule = New MappingModule()
    kernel.Load(mappingModule, loggingModule)

    'Because MyWrapper is a class within my web application (rather than an
    'external assembly which is handled in the above MappingModule) I have
    'to explicitly tell Ninject how to resolve it.
    kernel.Bind(Of MyWrapper).ToSelf().InRequestScope
End Sub

I edited MyWrapper class to include a new member variable and constructor

private ReadOnly _service As IService

Public Sub New(service As IService)
    _service = service
End Sub

So, with the above in place, I can now use the following code from my web page (MyPage.aspx.vb)


This now works.

As a sanity check, I also added code to the Constructor and Dispose methods of my implemention of IService and logged this out to see when the object were being created and disposed.

From the log, I can see that my Service object is correctly created and disposed of per request.