-
Notifications
You must be signed in to change notification settings - Fork 68
Add support to hook into MVC and WebApi DI frameworks #619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This change adds support for integrating the host's dependency injection into the ASP.NET Framework entrypoints. This is done by adding a source generator that will inject the appropriate hooks so we don't have to take a dependency on the actual frameworks. A user can consume this as simply as: ``` builder.AddSystemWebDependencyInjection(); ``` and it will be hooked up to all the DI frameworks (and it's extensible for others to add more if needed with the interface IDependencyRegistrar)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds dependency injection integration for ASP.NET Framework applications by hooking into MVC and Web API DI frameworks through source generation. Users can now enable DI with a single builder.AddSystemWebDependencyInjection() call.
Key Changes:
- Source generator that conditionally includes MVC/WebAPI DI hooks without hard dependencies
- Extension methods to retrieve request-scoped services consistently across Framework and Core
- Analyzer to prevent usage of the non-functional
HttpContext.GetServicemethod
Reviewed Changes
Copilot reviewed 24 out of 25 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextRequestServicesExtensions.cs | New extension methods for accessing request services across platforms |
| src/Microsoft.AspNetCore.SystemWebAdapters.FrameworkServices/WebObjectActivatorExtensions.cs | Refactored to use new IDependencyRegistrar pattern for multiple DI frameworks |
| src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp/FrameworkDependencyInjectionGenerator.cs | Source generator that creates DI registration code based on referenced assemblies |
| src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers/HttpContextDependencyAnalyzer.cs | Analyzer preventing use of non-functional HttpContext.GetService |
| samples/*/Global.asax.cs | Updated to use new AddSystemWebDependencyInjection() method |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextRequestServicesExtensions.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers/CompilationExtensions.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.AspNetCore.SystemWebAdapters/HttpContextRequestServicesExtensions.cs
Show resolved
Hide resolved
| [Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "<Pending>")] | ||
| public static class HttpContextRequestServicesExtensions | ||
| { | ||
| public static IServiceProvider GetRequestServices(this HttpContext context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be possible to use the new extension members format and make this a property called RequestServices to more closely align with the new Core api?
I've tested it briefly and it does seem to compile and work, but VS2022 IDE is not happy - seemed okay in VS2026 though.
Maybe this will be more possible when the net 10 sdk is released....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like that, but I don't think we currently have anything that would require using something greater than 7.3 on framework apps, and I don't really want to start now.
If we've used things that require using > c# 7.3, then I may be more ok with it.
This change adds support for integrating the host's dependency injection into the ASP.NET Framework entrypoints. This is done by adding a source generator that will inject the appropriate hooks so we don't have to take a dependency on the actual frameworks.
A user can consume this as simply as:
and it will be hooked up to all the DI frameworks (and it's extensible for others to add more if needed with the interface IDependencyRegistrar)
This also adds the following:
HttpContext.GetRequestServices()that will work on framework or coreIServiceProviderfromHttpContextwhich does absolutely nothing and can't be overridden or extended.