Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

The example given is in C#. I am working on a legacy VB.NET app that is in the process of being modernized. 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.

(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.

This seems to be due to the way that VB.NET handles constructors and inheritance. With the way VB.NET handles constructors is having parameterized constructors in a VB.NET WebForms project even possible?

like image 891
Bradford Dillon Avatar asked Nov 08 '22 06:11

Bradford Dillon


1 Answers

Did you target the app to 4.7.2 in web.config? 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.

<httpRuntime targetFramework="4.7.2"/>

Edit: Here is a blog post about how to use Unity in the exiting WebForms app.

like image 154
mattfei Avatar answered Nov 14 '22 22:11

mattfei