/AspNetDependencyInjection

`AspNetDependencyInjection` allows "Classic" ASP.NET Web Forms applications to use `Microsoft.Extensions.DependencyInjection` to compose application services using dependency injection.

Primary LanguageC#MIT LicenseMIT

AspNetDependencyInjection

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:

Background

  • 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 all WebControls 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 single OnLoad method and adding constructor injected dependencies - which means that each Page behaves almost identically to an ASP.NET Core Controller 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 using Unity instead of MEDI, located at https://github.com/Jehoel/Unity.WebForms.

  • This particular package's main objective is supporting ASP.NET Web Forms 4.7.2's new WebObjectActivator which means that Page, 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.

Supported ASP.NET platforms

  • 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 the Jehoel.AspNetDependencyInjection.Mvc NuGet package.
    • ASP.NET SignalR - By subclassing Microsoft.AspNet.SignalR.DefaultDependencyResolver. Has optional support for IServiceScope. Use the Jehoel.AspNetDependencyInjection.SignalR NuGet package.
    • ASP.NET Web API - Using System.Web.Http.Dependencies.IDependencyResolver. Use the Jehoel.AspNetDependencyInjection.WebApi NuGet package.
    • WCF is not currently targeted by a published NuGet package, but a WCF example is included in this repository.

Current Version

  • 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 to AspNetDependencyInjection (as it now is unrelated to Unity.
  • 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.

NuGet Gallery

Install-Package Jehoel.AspNetDependencyInjection

Install-Package Jehoel.AspNetDependencyInjection.Mvc

Install-Package Jehoel.AspNetDependencyInjection.SignalR

Install-Package Jehoel.AspNetDependencyInjection.WebApi

Installation and Getting Started

Please see the GETTING_STARTED.md file in the GitHub repository: https://github.com/Jehoel/AspNetDependencyInjection/blob/master/GETTING_STARTED.md

References / Links

Here are some of the sources used for building out this package:

Copyright

License

AspNetDependencyInjection and all related prior projects are licensed under the MIT license - http://www.opensource.org/licenses/mit-license