简体   繁体   English

在.NET 4.7.2下使用VB.NET在WebForms中进行构造函数注入

[英]Constructor injection in WebForms using VB.NET under .NET 4.7.2

In .NET 4.7.2 Microsoft introduced the ability to use constructor injection in WebForms through the use of HttpRuntime.WebObjectActivator as seen here 在.NET 4.7.2微软推出通过使用使用构造函数注入在WebForms的能力HttpRuntime.WebObjectActivator所看到这里

The example given is in C#. 给出的示例在C#中。 I am working on a legacy VB.NET app that is in the process of being modernized. 我正在开发一个正在升级的旧版VB.NET应用程序。 We would like to use constructor injection over the property injection we had been planning on using because it is more canonical. 我们想使用构造函数注入而不是我们计划使用的属性注入,因为它更加规范。

However, when I attempt to give a WebForm a parameterized constructor I get the following error. 但是,当我尝试为WebForm提供参数化的构造函数时,出现以下错误。

(BC30387) Class 'testpage_aspx' must declare a 'Sub New' because its base class 'TestPage' does not have an accessible 'Sub New' that can be called with no arguments. (BC30387)类'testpage_aspx'必须声明一个'Sub New',因为它的基类'TestPage'没有可调用的,没有参数的'Sub New'。

This seems to be due to the way that VB.NET handles constructors and inheritance. 这似乎是由于VB.NET处理构造函数和继承的方式引起的。 With the way VB.NET handles constructors is having parameterized constructors in a VB.NET WebForms project even possible? 通过VB.NET处理构造函数的方式,甚至可以在VB.NET WebForms项目中使用参数化构造函数吗?

Did you target the app to 4.7.2 in web.config? 您是否在web.config中将应用程序定位到4.7.2? Something like below. 像下面这样。 Constructor injection can't be used in Page/Controls in website project(the project type that doesn't have vbproj file), since the base type of the Page/Controls are unknown when generating code for Pages/Controls. 构造函数注入不能用于网站项目(没有vbproj文件的项目类型)的页面/控件中,因为在为页面/控件生成代码时页面/控件的基本类型是未知的。

<httpRuntime targetFramework="4.7.2"/>

Edit: Here is a blog post about how to use Unity in the exiting WebForms app. 编辑:这是有关如何在现有WebForms应用程序中使用Unity的博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM