0

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)()
TheMook
  • 1,531
  • 4
  • 20
  • 58
  • Does `lbdUOW` have parameterless constructor? – mipe34 Jan 14 '13 at 19:15
  • Edited main question as comments don't allow formatting. – TheMook Jan 14 '13 at 20:20
  • The generic exception says that you did not provide the required argument for constructor. It seems to me that Ninject was not involved there so far. Try check these examples http://stackoverflow.com/questions/4933695/how-can-i-implement-ninject-or-di-on-asp-net-web-forms if you have done all steps required. – mipe34 Jan 14 '13 at 20:51
  • @mipe34 Thanks but that link is where I started and it goes on to say that first bit is outdated and now the createkernel stuff is all done in the ninjectcommon file. As I wrote above, I have put a debug stop in the registerservices part of ninjectcommon and the code hits that stop point before I get the error. It seems more to me that the ninject process is not injecting the uow interface as required? Is there any way of debugging this in more detail so I can see if ninject is doing its job? – TheMook Jan 14 '13 at 22:41

1 Answers1

2

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

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • Thanks Remo. Do you have a link to an example of this please as I am new to ninject and I'm not sure how to accomplish this. – TheMook Jan 15 '13 at 08:26
  • Just create `lbdUOW` property on your page an put `[Inject]` attribute on it - and remove it from constructor. If you had read this article (http://stackoverflow.com/questions/4933695/how-can-i-implement-ninject-or-di-on-asp-net-web-forms) well you would see that they are using property injection instead of constructor injection. ;-) – mipe34 Jan 15 '13 at 08:37
  • You're right, sorry. It's early in the morning and this is all so new to me that I wasn't thinking straight. I have been switching between classes and pages and didn't even realise I'd got a "new()" constructor copied into a web form! Have replaced that now with a public property with the [inject] attribute and all is working, many thanks. A very helpful article to me in understanding the way this all works and may be helpful for other noobs is here: http://www.codetunnel.com/blog/post/58/how-to-implement-proper-separation-of-concerns-in-your-web-forms-application – TheMook Jan 15 '13 at 08:51
  • This was certainly true in 2013, but for anyone reading this today, with [.Net framework 4.7.2](https://devblogs.microsoft.com/dotnet/announcing-the-net-framework-4-7-2/) support for dependency injection was added. – R. Schreurs Nov 29 '22 at 08:46