Dependency Injection now available in Web Forms via Unity
Step 1: Upgrade all the projects in your solution to target at least .NET Framework 4.7.2
This feature is only available starting at 4.7.2, so you’ll need to target 4.7.2 across all your projects. In Visual Studio that means right clicking the project, clicking Properties, and in the Application section, select .NET 4.7.2 from the Target Framework dropdown.
You’ll also need to set the target framework in your web.config file as an attribute of the httpRuntime element.
<system.web>
<httpRuntime targetFramework="4.7.2" />
...
When I made this change, I was already at 4.7.1 so it was mostly a seamless upgrade, but you’ll want to be sure to compile your projects and iron out any build errors.
Possible Gotcha: Disabling unobtrusive validation mode
I can’t say for certain if the upgrade caused this issue, but soon after I did it, I noticed some errors appearing related to javascript on some of my existing pages, and the solution was to disable a newly defaulted feature known as Unobtrusive Validation Mode.
To disable this feature, add None as the value of this key in your web.config file.
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
Step 2: Install the Microsoft.AspNet.WebFormsDependencyInjection.Unity NuGet Package to your web application project (the project that your Global.asax.cs file resides.)
This is the Unity package that will allow you to inject dependencies at application startup time.
Step 3: Add the Unity container to the Global.asax.cs file and register a type for injection.
Within your Global.asax.cs file, you’ll need to add these two using references:
using Microsoft.AspNet.WebFormsDependencyInjection.Unity;
using Unity;
Then in your Application_Start method, add the call to create the container and register a type:
protected void Application_Start(Object sender, EventArgs e)
{
var container = this.AddUnity();
container.RegisterType<IQuoteService, FakeQuoteService>();
}
In this example, whenever an IQuoteService is used as a constructor argument for one of your pages or controls, a FakeQuoteService will be injected.
Step 4: Use the interface within a page or user control.
To reap the benefits, you simply need to add the interface as a constructor parameter like so:
private IQuoteService _myQuoteService;
public QuotePage(IQuoteService myQuoteService)
{
_myQuoteService = myQuoteService;
}
If everything is set up correctly, this page will be working with a FakeQuoteService at runtime yet conveniently hidden behind an interface.
Quirk: A build error appears intermittently
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.