vendredi 7 octobre 2016

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

<Inject>
Public Property _myService As IService

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

_myService.DoWork()

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

Public Class MyWrapper

    <Inject>
    Public Property _myService As IService

    Public Sub DoWork()
        _myService.DoWork()
    End Sub

End Class

This class is used by web pages...

Dim wrapper As New MyWrapper()

wrapper.DoWork()

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.




Aucun commentaire:

Enregistrer un commentaire