Skip to content

Conversation

@twsouthwick
Copy link
Member

@twsouthwick twsouthwick commented Oct 22, 2025

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)

This also adds the following:

  • A simple way to get request scoped dependencies from HttpContext.GetRequestServices() that will work on framework or core
  • An analyzer to ensure people don't try to use the IServiceProvider from HttpContext which does absolutely nothing and can't be overridden or extended.

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)
@twsouthwick twsouthwick requested a review from Copilot October 22, 2025 17:15
Copy link

Copilot AI left a 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.GetService method

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.

@twsouthwick twsouthwick enabled auto-merge (squash) October 22, 2025 17:41
[Diagnostics.CodeAnalysis.SuppressMessage("Maintainability", "CA1510:Use ArgumentNullException throw helper", Justification = "<Pending>")]
public static class HttpContextRequestServicesExtensions
{
public static IServiceProvider GetRequestServices(this HttpContext context)
Copy link
Contributor

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....

Copy link
Member Author

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.

@twsouthwick twsouthwick merged commit c8a44bc into main Oct 27, 2025
5 checks passed
@twsouthwick twsouthwick deleted the di-gen branch October 27, 2025 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants