“Argument not specified for parameter” using Ninje

2019-08-17 22:15发布

Just to re-iterate the title, this is web forms and not mvc. Also it's using vb.net.

I have used nuget to add ninject.web to my project. I have configured NinjectWebCommon's "RegisterServices" method as follows:

Private Shared Sub RegisterServices(kernel As IKernel)
    kernel.Bind(Of ilbdUOW)().[To](Of lbdUOW)()
End Sub

Everything compiles clean. When I run the simple web form (.aspx page, no master page), I get the following error:

BC30455: Argument not specified for parameter '_uow' of 'Public Sub New(_uow As lbD.data.lbdUOW)'.

And it points to line 52 in the compiled code:

Line 51:         Public Sub New()
Line 52:             MyBase.New
Line 53:             Dim dependencies() As String

which (I believe) comes from the top of the .aspx page when compiled, where I have the following code for the "new" constructor:

<Inject()> _
Public Sub New(_uow As ilbdUOW)
    Uow = _uow
End Sub

(well, I say "I believe" but I know it is as if I change the "ilbdUOW" from the interface to the class of "lbdUOW" it changes the error message accordingly so that's definitely where it's coming from)

If I put a debug stop on the "uow = _uow" line in my "New" constructor and also another debug stop in the "RegisterServices" method in the NinjectWebCommon file, the debugger only stops on the "RegisterServices" debug point and never gets to hit the debug stop in the main .aspx page.

I'm now completely lost. I am trying to get my head around what is relatively new to me and I thought I was pretty much getting there. My understanding is that the code in "RegisterServices" in NinjectWebCommon will recognise that I'm requiring an instance of my "ilbdUOW" interface when it is referenced in a constructor (such as my "new" constructor in my .aspx page) and will inject it automatically, thus making the unit of work instance available for me to access the database with.

If anyone can point me in the right direction I'd be really grateful, thanks.

Edited to add: @mipe34

Not on the "new" constructor, no:

Public Sub New(ByVal repositoryProvider As IRepositoryProvider)
    MyBase.New()
    CreateDbContext()
    repositoryProvider.DbContext = DbContext
    _RepositoryProvider = repositoryProvider
End Sub

I have also set up the "RegisterServices" section to map those interfaces too, but it makes no difference to the error I get whether those extra mappings are in there or not, it seems to fail before I get to that point.

I have had these lines in and out of the "RegisterServices" section and it makes no difference:

    kernel.Bind(Of RepositoryFactories)().[To](Of RepositoryFactories)().InSingletonScope()
    kernel.Bind(Of IRepositoryProvider)().[To](Of RepositoryProvider)()

1条回答
淡お忘
2楼-- · 2019-08-17 23:12

WebForm are created by ASP.NET not Ninject You can't use constructor injection on WebForms. You have to do property injection instead.

查看更多
登录 后发表回答