AspNetDependencyInjection allows "Classic" ASP.NET Web Forms applications to use Microsoft.Extensions.DependencyInjection
("MEDI") to inject dependencies directly into System.Web.UI.Page
, UserControl
, and MasterPage
constructors (and more!) all thanks to WebObjectActivator
. (WebObjectActivator
is a new feature in ASP.NET WebForms 4.7.2).
Currently listed on NuGet.org as:
Jehoel.AspNetDependencyInjection
Jehoel.AspNetDependencyInjection.Mvc
Jehoel.AspNetDependencyInjection.SignalR
Jehoel.AspNetDependencyInjection.WebApi
-
I'm working on a large ASP.NET WebForms application written almost a decade ago that needs to be moved to ASP.NET Core - I felt the best approach to transition is to work on a page-by-page basis: first updating each
.aspx
page to replace any and allWebControls
with direct rendering (using<%=
and<%:
with data from a ViewModel-object), and then updating the CodeBehind.aspx.cs
to remove Control event handlers so the page has only a singleOnLoad
method and adding constructor injected dependencies - which means that eachPage
behaves almost identically to an ASP.NET CoreController
which makes the eventual transition to ASP.NET MVC or ASP.NET Core far easier. -
This project is derived from my earlier
Unity.WebForms
project which provided DI usingUnity
instead of MEDI, located at https://github.com/Jehoel/Unity.WebForms.- ...which itself is a derivative and fork of S. Kyle Korndoerfer's original project at https://bitbucket.org/KyleK/unity.webforms
- ...which cites DevTrends Unity.MVC3 and DevTrends Unity.WCF as original works.
- ...which itself is a derivative and fork of S. Kyle Korndoerfer's original project at https://bitbucket.org/KyleK/unity.webforms
-
This particular package's main objective is supporting ASP.NET Web Forms 4.7.2's new
WebObjectActivator
which means thatPage
,UserControl
and other types can use true constructor dependency injection. Previously applications had to use "property injection" which many consider to be an anti-pattern.
- This project now targets:
- ASP.NET WebForms - This is possible thanks to
System.Web.HttpRuntime.WebObjectActivator
, which means you need to be running ASP.NET on the .NET Framework 4.7.2 or later (including .NET Framework 4.8). - ASP.NET MVC - Using MVC's
System.Web.Mvc.IDependencyResolver
. Use theJehoel.AspNetDependencyInjection.Mvc
NuGet package. - ASP.NET SignalR - By subclassing
Microsoft.AspNet.SignalR.DefaultDependencyResolver
. Has optional support forIServiceScope
. Use theJehoel.AspNetDependencyInjection.SignalR
NuGet package. - ASP.NET Web API - Using
System.Web.Http.Dependencies.IDependencyResolver
. Use theJehoel.AspNetDependencyInjection.WebApi
NuGet package. - WCF is not currently targeted by a published NuGet package, but a WCF example is included in this repository.
- ASP.NET WebForms - This is possible thanks to
- 1.4 - S. Kyle Korndoerfer's most recent version of
Unity.WebForms
, released in 2015. See https://bitbucket.org/KyleK/unity.webforms. - 2.0 - My
Unity.WebForms
project, updated in 2019 for ASP.NET 4.7.2 and WebObjectActivator). See https://github.com/Jehoel/Unity.WebForms. - 3.0 - After being extensively modified to use
Microsoft.Extensions.DependencyInjection
and renamed toAspNetDependencyInjection
(as it now is unrelated toUnity
. - 3.2 - Adding support for ASP.NET MVC (which turned out to be broken).
- 3.3 - Bugfixing ASP.NET MVC support.
- 4.0 - Major refactoring and API redesign. Adding support for SignalR (both scoped and unscoped) and ASP.NET Web API.
Install-Package Jehoel.AspNetDependencyInjection
Install-Package Jehoel.AspNetDependencyInjection.Mvc
Install-Package Jehoel.AspNetDependencyInjection.SignalR
Install-Package Jehoel.AspNetDependencyInjection.WebApi
Please see the GETTING_STARTED.md
file in the GitHub repository: https://github.com/Jehoel/AspNetDependencyInjection/blob/master/GETTING_STARTED.md
Here are some of the sources used for building out this package:
Unity.WebForms
:- Dependency Injection in ASP.NET WebForms - Mikael Lundin
- Unity: How to registerType with a PARAMETER constructor
- Unity.MVC3 - The linked articles on the bottom helped a lot with infrastructure
Jehoel.Unity.AspNetWebForms
:- Announcing the .NET Framework 4.7.2 - This article announced support for constructor-based DI.
- Use Dependency Injection In WebForms Application
- AspNetWebFormsDependencyInjection on Github
- Sample example of using AspNetWebFormsDependencyInjection - (Repository snapshot)
- Official Unity project for ASP.NET MVC
- Copyright 2013 - 2015 S. Kyle Korndoerfer
- Copyright 2019 Dai Rees
AspNetDependencyInjection
and all related prior projects are licensed under the MIT license - http://www.opensource.org/licenses/mit-license