Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net ASP.NET Web表单+;Ninject+;使用iService的Helper类-如何使Ninject处理iService?_Vb.net_Web_Dependency Injection_Webforms_Ninject.web - Fatal编程技术网

Vb.net ASP.NET Web表单+;Ninject+;使用iService的Helper类-如何使Ninject处理iService?

Vb.net ASP.NET Web表单+;Ninject+;使用iService的Helper类-如何使Ninject处理iService?,vb.net,web,dependency-injection,webforms,ninject.web,Vb.net,Web,Dependency Injection,Webforms,Ninject.web,我有一个ASP.NET Web窗体项目,它是用VB.NET编写的,运行在.NET 4.5.2上 我已成功地将Ninject插入我的应用程序,但遇到了一个障碍 我在我的网页中使用Ninject属性注入(下面的示例) 但是,在我的应用程序中,我们有一个类,用于将调用包装到服务中(见下文) 这不起作用-在MyWrapper类中,Ninject没有注入IService 我猜,因为我的类不是一个网页,它不在管道中,所以Ninject没有用它做任何事情 我读过其他一些帖子,建议修改MyWrapper以从Ni

我有一个ASP.NET Web窗体项目,它是用VB.NET编写的,运行在.NET 4.5.2上

我已成功地将Ninject插入我的应用程序,但遇到了一个障碍

我在我的网页中使用Ninject属性注入(下面的示例)

但是,在我的应用程序中,我们有一个类,用于将调用包装到服务中(见下文)

这不起作用-在MyWrapper类中,Ninject没有注入IService

我猜,因为我的类不是一个网页,它不在管道中,所以Ninject没有用它做任何事情

我读过其他一些帖子,建议修改MyWrapper以从Ninject.Web.PageBase继承,但尝试过之后,它似乎不起作用

有人有什么建议吗

MyWrapper在我们的应用程序中有100多个引用,所以理想情况下,我不想更改每一行代码

另外,MyWrapper类有一些共享/静态方法——这会导致问题吗(如果必要,我可以更改)

仅供参考-上述代码应被视为伪代码-我是临时键入的,而不是复制粘贴的-因此它可能包含语法错误。

最终解决了它

所以,我无法让我的包装器类使用Ninject。我在这里的假设是,因为这是一个web表单应用程序(而不是MVC),管道是不同的,MyWrapper类不是通过同一管道运行的,所以Ninject从来没有意识到这一点

以下是我为“修复”我的问题所做的

我创建了一个新的基本页面

Public Class MyBasePage
    Inherits System.Web.UI.Page

    <Inject>
    Public Property wrapper As MyWrapper
End Class
我之前忘记提到我已经将MyWrapper添加到NinjectWebCommon.vb文件中

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
我编辑了MyWrapper类以包含一个新的成员变量和构造函数

private ReadOnly _service As IService

Public Sub New(service As IService)
    _service = service
End Sub
因此,有了上述内容,我现在可以从我的网页(MyPage.aspx.vb)中使用以下代码

这现在起作用了

作为一项健全性检查,我还向IService实现的构造函数和Dispose方法添加了代码,并将其注销,以查看对象的创建和处置时间

从日志中,我可以看到我的服务对象已正确创建并按请求处理。

最终解决了这个问题

所以,我无法让我的包装器类使用Ninject。我在这里的假设是,因为这是一个web表单应用程序(而不是MVC),管道是不同的,MyWrapper类不是通过同一管道运行的,所以Ninject从来没有意识到这一点

以下是我为“修复”我的问题所做的

我创建了一个新的基本页面

Public Class MyBasePage
    Inherits System.Web.UI.Page

    <Inject>
    Public Property wrapper As MyWrapper
End Class
我之前忘记提到我已经将MyWrapper添加到NinjectWebCommon.vb文件中

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
我编辑了MyWrapper类以包含一个新的成员变量和构造函数

private ReadOnly _service As IService

Public Sub New(service As IService)
    _service = service
End Sub
因此,有了上述内容,我现在可以从我的网页(MyPage.aspx.vb)中使用以下代码

这现在起作用了

作为一项健全性检查,我还向IService实现的构造函数和Dispose方法添加了代码,并将其注销,以查看对象的创建和处置时间

从日志中,我可以看到每个请求都正确地创建和处理了我的服务对象

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
private ReadOnly _service As IService

Public Sub New(service As IService)
    _service = service
End Sub
wrapper.DoWork()