I used article "Use Dependency Injection In WebForms Application" https://devblogs.microsoft.com/aspnet/use-dependency-injection-in-webforms-application/ The project retargeted to .NET Framework 4.7.2 in project properties and in web.config:
<system.web>
<httpRuntime targetFramework="4.72" ...
AspNet.WebFormsDependencyInjection.Unity NuGet package is installed.
Type is registered in Global:
public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
var container = this.AddUnity();
container.RegisterType<IVCRole, eVCRole>();
}
...
I checked container and it is working and registering interface IVCRole mapping to class eVCRole. Default.aspx.cs is refactored:
public partial class Default : System.Web.UI.Page
{
private IVCRole vcr;
public Default(IVCRole avcr)
{
vcr = avcr;
}
protected void Page_Load(object sender, EventArgs e)
{
...
But when I run web application there is an error "Constructor on type 'ASP.default_aspx' not found." If I add this constructor:
public partial class Default : System.Web.UI.Page
{
private IVCRole vcr;
public Default() {}
public Default(IVCRole avcr)
{
vcr = avcr;
}
protected void Page_Load(object sender, EventArgs e)
{
...
the constructor for DI
public Default(IVCRole avcr)
{
vcr = avcr;
}
is never called and "vcr" is always null in Page_Load. There is an article: "Dependency Injection in ASP.NET Web Forms": https://makingloops.com/dependency-injection-in-web-forms/ where this error is mentioned: "On occasion you may see a build error complaining about the lack of a zero-argument constructor on the page. I notice that this error will magically go away depending on the context. Someone else suggested using property injection with the Dependency attribute on pages to get around this, but i didn’t find that was necessary." But in my case there is no "magic". There is similar question in Stackoverflow: .NET 4.7.2 Dependency Injection in ASP.NET WebForms Website - Constructor injection not working But in my case property injection is not working:
public partial class Default : System.Web.UI.Page
{
[Dependency]
public IVCRole vcr { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
...
"vcr" in Page_Load is still null. There is solution to get it working with custom implementation of DI provider but I already using .NET 4.7.2 an Unity. Author mentioned that for web application should not be any problem as the problem is with website compiler. How to get DI constructor or property injection to working in Default page using .NET 4.7.2 and Unity?
This is Stack:
[MissingMethodException: Constructor on type 'ASP.default_aspx' not found.]
System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark) +1173
System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes) +130
System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture) +21
Microsoft.AspNet.WebFormsDependencyInjection.Unity.ContainerServiceProvider.DefaultCreateInstance(Type type) +17
Microsoft.AspNet.WebFormsDependencyInjection.Unity.ContainerServiceProvider.GetService(Type serviceType) +161
__ASP.FastObjectFactory_app_web_mmaneivx.Create_ASP_default_aspx() in c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs\19e4d468\8c7800a0\App_Web_mmaneivx.2.cs:0
System.Web.Compilation.BuildResultCompiledType.CreateInstance() +31
System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp) +104
System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) +33
System.Web.UI.PageHandlerFactory.GetHandler(HttpContext context, String requestType, String virtualPath, String path) +39
System.Web.MaterializeHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +386
System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163