在Web表单使用Ninject

在Web表单使用Ninject

本文介绍了"参数参数&QUOT没有规定;在Web表单使用Ninject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是再次重申了冠军,这是网页形式,而不是MVC。同时它使用vb.net。

我用的NuGet来ninject.web添加到我的项目。我已经配置NinjectWebCommon的RegisterServices的方法如下:

 私人共享子RegisterServices(内核的iKernel)
    kernel.Bind(中ilbdUOW)()。[转](中lbdUOW)()
结束小组

一切编译干净。当我运行简单的Web窗体(.aspx页面,没有母版页),我收到以下错误:

  BC30455:为公共的Sub New(_uow作为lbD.data.lbdUOW)'参数'_uow没有指定的参数。

和它指向的编译code排队52:

  51号线:的Public Sub New()
第52行:MyBase.New
53号线:昏暗的依赖关系()作为字符串

在编译时,这(我相信)来自在.aspx页面的顶部

在那里我有以下的code为新的构造:

 <注入()> _
公共子新(_uow作为ilbdUOW)
    UOW = _uow
结束小组

(当然,我说的我相信,但我知道这是因为如果我改变ilbdUOW从接口的类lbdUOW它相应地更改错误信息,这样肯定是从哪儿来)

如果我把一个调试停在我的新的构造在NinjectWebCommon文件中的RegisterServices方法UOW = _uow行,另一种调试停止,调试器只能停在RegisterServices调试点并永远不会打调试停在主.aspx页面中。

我现在完全丧失。我试图让我身边的是相对较新的我的头,我想我是pretty多到达那里。我的理解是,在NinjectWebCommonRegisterServices的code将承认,当它在一个构造函数(引用了我需要我的ilbdUOW接口的一个实例,如我在.aspx页面中我的新的构造),并自动将它注入,从而使工作实例的单元可供我与访问数据库。

如果任何人都可以点我在正确的方向,我会非常感激,感谢。

编辑补充:
@ mipe34

没有对新的构造,没有:

 的Public Sub New(BYVAL repositoryProvider作为IRepositoryProvider)
    MyBase.New()
    CreateDbContext()
    repositoryProvider.DbContext =的DbContext
    _RepositoryProvider = repositoryProvider
结束小组

我还成立了RegisterServices部分这些接口也映射,但它使到错误,我得到这些额外的映射是否在那里与否没有区别,看来之前我到这一点失败。

我曾在进出RegisterServices一节中的这些线,它没有什么区别:

  kernel.Bind(RepositoryFactories)的()。[转](中RepositoryFactories)()。InSingletonScope()
    kernel.Bind(中IRepositoryProvider)()。[转](中RepositoryProvider)()


解决方案

Web窗体由ASP.NET创建不Ninject您不能使用的WebForms构造函数注入。你所要做的财产,而不是注射

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)()
解决方案

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

这篇关于&QUOT;参数参数&QUOT没有规定;在Web表单使用Ninject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:45