Skip to content

Commit c8a44bc

Browse files
authored
Add support to hook into MVC and WebApi DI frameworks (#619)
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.
1 parent 6ff4988 commit c8a44bc

File tree

82 files changed

+1636
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1636
-71
lines changed

Microsoft.AspNetCore.SystemWebAdapters.sln

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Microsoft Visual Studio Solution File, Format Version 12.00
2-
# Visual Studio Version 17
3-
VisualStudioVersion = 17.1.32127.271
2+
# Visual Studio Version 18
3+
VisualStudioVersion = 18.3.11122.13 main
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SystemWebAdapters", "src\Microsoft.AspNetCore.SystemWebAdapters\Microsoft.AspNetCore.SystemWebAdapters.csproj", "{55C1BBE0-B922-46B0-8F2C-8472BC9A5F33}"
66
EndProject
@@ -124,6 +124,16 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppConfigFramework", "sampl
124124
EndProject
125125
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AppConfigCore", "samples\AppConfig\AppConfigCore\AppConfigCore.csproj", "{6ECA1F1C-214B-BB97-27D7-319C7F1AD90F}"
126126
EndProject
127+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.SystemWebAdapters.Analyzers", "src\Microsoft.AspNetCore.SystemWebAdapters.Analyzers\Microsoft.AspNetCore.SystemWebAdapters.Analyzers.csproj", "{7A244BE9-B12E-424F-A1F1-E386C43BF0A9}"
128+
EndProject
129+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp", "src\Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp\Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp.csproj", "{904FF6F2-0322-9B85-7FBA-C512196D72C2}"
130+
EndProject
131+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "DependencyInjection", "DependencyInjection", "{BCEFA0EE-4AC7-417E-A861-368DB3D661B1}"
132+
EndProject
133+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjectionAppHost", "samples\DependencyInjection\DependencyInjectionAppHost\DependencyInjectionAppHost.csproj", "{B447F0F6-B232-D326-334B-0095BA74F988}"
134+
EndProject
135+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DependencyInjectionFramework", "samples\DependencyInjection\DependencyInjectionFramework\DependencyInjectionFramework.csproj", "{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F}"
136+
EndProject
127137
Global
128138
GlobalSection(SolutionConfigurationPlatforms) = preSolution
129139
Debug|Any CPU = Debug|Any CPU
@@ -278,6 +288,22 @@ Global
278288
{6ECA1F1C-214B-BB97-27D7-319C7F1AD90F}.Debug|Any CPU.Build.0 = Debug|Any CPU
279289
{6ECA1F1C-214B-BB97-27D7-319C7F1AD90F}.Release|Any CPU.ActiveCfg = Release|Any CPU
280290
{6ECA1F1C-214B-BB97-27D7-319C7F1AD90F}.Release|Any CPU.Build.0 = Release|Any CPU
291+
{7A244BE9-B12E-424F-A1F1-E386C43BF0A9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
292+
{7A244BE9-B12E-424F-A1F1-E386C43BF0A9}.Debug|Any CPU.Build.0 = Debug|Any CPU
293+
{7A244BE9-B12E-424F-A1F1-E386C43BF0A9}.Release|Any CPU.ActiveCfg = Release|Any CPU
294+
{7A244BE9-B12E-424F-A1F1-E386C43BF0A9}.Release|Any CPU.Build.0 = Release|Any CPU
295+
{904FF6F2-0322-9B85-7FBA-C512196D72C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
296+
{904FF6F2-0322-9B85-7FBA-C512196D72C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
297+
{904FF6F2-0322-9B85-7FBA-C512196D72C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
298+
{904FF6F2-0322-9B85-7FBA-C512196D72C2}.Release|Any CPU.Build.0 = Release|Any CPU
299+
{B447F0F6-B232-D326-334B-0095BA74F988}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
300+
{B447F0F6-B232-D326-334B-0095BA74F988}.Debug|Any CPU.Build.0 = Debug|Any CPU
301+
{B447F0F6-B232-D326-334B-0095BA74F988}.Release|Any CPU.ActiveCfg = Release|Any CPU
302+
{B447F0F6-B232-D326-334B-0095BA74F988}.Release|Any CPU.Build.0 = Release|Any CPU
303+
{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
304+
{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
305+
{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
306+
{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F}.Release|Any CPU.Build.0 = Release|Any CPU
281307
EndGlobalSection
282308
GlobalSection(SolutionProperties) = preSolution
283309
HideSolutionNode = FALSE
@@ -328,6 +354,11 @@ Global
328354
{16A9239C-ADC1-B9FA-8C50-081E2A420B5B} = {012BEA62-0FF1-472E-AD5F-0D2029336701}
329355
{90FE21BE-F292-A973-D281-9D4DC81CEB6F} = {012BEA62-0FF1-472E-AD5F-0D2029336701}
330356
{6ECA1F1C-214B-BB97-27D7-319C7F1AD90F} = {012BEA62-0FF1-472E-AD5F-0D2029336701}
357+
{7A244BE9-B12E-424F-A1F1-E386C43BF0A9} = {F9DB9323-C919-49E8-8F96-B923D2F42E60}
358+
{904FF6F2-0322-9B85-7FBA-C512196D72C2} = {F9DB9323-C919-49E8-8F96-B923D2F42E60}
359+
{BCEFA0EE-4AC7-417E-A861-368DB3D661B1} = {95915611-30BF-4AFF-AE41-5CDC6F57DCF7}
360+
{B447F0F6-B232-D326-334B-0095BA74F988} = {BCEFA0EE-4AC7-417E-A861-368DB3D661B1}
361+
{4A2022AB-1ECE-E630-FD48-DD045DB4DD9F} = {BCEFA0EE-4AC7-417E-A861-368DB3D661B1}
331362
EndGlobalSection
332363
GlobalSection(ExtensibilityGlobals) = postSolution
333364
SolutionGuid = {DABA3C65-9D74-4EB6-9B1C-730328710EAD}

samples/AppConfig/AppConfigAppHost/AppConfigAppHost.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Sdk Name="Aspire.AppHost.Sdk" />
44

55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
7-
<TargetFramework>net10.0-windows</TargetFramework>
7+
<TargetFramework>$(SAMPLES_TFM)-windows</TargetFramework>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<Nullable>enable</Nullable>
1010
<IsAspireHost>true</IsAspireHost>

samples/AppConfig/AppConfigCore/AppConfigCore.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>net9.0</TargetFramework>
3+
<TargetFramework>$(SAMPLES_TFM)</TargetFramework>
44
<Nullable>enable</Nullable>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
@@ -15,4 +15,4 @@
1515
<ItemGroup>
1616
<ProjectReference Include="..\..\ServiceDefaults\Samples.ServiceDefaults.csproj" />
1717
</ItemGroup>
18-
</Project>
18+
</Project>

samples/AppConfig/AppConfigCore/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Microsoft.AspNetCore.Http.Features;
21
using Microsoft.AspNetCore.SystemWebAdapters;
3-
using Microsoft.AspNetCore.SystemWebAdapters.Features;
42

53
var builder = WebApplication.CreateBuilder(args);
64

samples/AppConfig/AppConfigFramework/Global.asax.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected void Application_Start()
1111
HttpApplicationHost.RegisterHost(builder =>
1212
{
1313
builder.AddServiceDefaults();
14-
builder.RegisterWebObjectActivator();
14+
builder.AddSystemWebDependencyInjection();
1515
});
1616
}
1717
}

samples/AppConfig/AppConfigFramework/Web.config

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<connectionStrings>
44
<add name="ConnStr1" connectionString="web.config" />
55
<add name="ConnStr2" connectionString="web.config" />
66
</connectionStrings>
77
<appSettings>
88
<add key="Setting1" value="web.config" />
9-
<add key="Setting2" value="web.config"/>
9+
<add key="Setting2" value="web.config" />
1010
</appSettings>
1111
<system.webServer>
1212
<modules runAllManagedModulesForAllRequests="true">
@@ -154,4 +154,10 @@
154154
</dependentAssembly>
155155
</assemblyBinding>
156156
</runtime>
157+
<system.codedom>
158+
<compilers>
159+
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
160+
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
161+
</compilers>
162+
</system.codedom>
157163
</configuration>

samples/AuthRemoteFormsAuth/AuthRemoteFormsAuthAppHost/AuthRemoteFormsAuthAppHost.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Sdk Name="Aspire.AppHost.Sdk" />
44

55
<PropertyGroup>
66
<OutputType>Exe</OutputType>
7-
<TargetFramework>net10.0-windows</TargetFramework>
7+
<TargetFramework>$(SAMPLES_TFM)-windows</TargetFramework>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<Nullable>enable</Nullable>
1010
<IsAspireHost>true</IsAspireHost>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFramework>net10.0</TargetFramework>
3+
<TargetFramework>$(SAMPLES_TFM)</TargetFramework>
44
<Nullable>enable</Nullable>
55
<ImplicitUsings>enable</ImplicitUsings>
66
</PropertyGroup>
77
<ItemGroup>
88
<ProjectReference Include="..\..\ServiceDefaults\Samples.ServiceDefaults.csproj" />
99
</ItemGroup>
10-
</Project>
10+
</Project>

samples/AuthRemoteFormsAuth/AuthRemoteFormsAuthFramework/Global.asax.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected void Application_Start()
1717
HttpApplicationHost.RegisterHost(builder =>
1818
{
1919
builder.AddServiceDefaults();
20-
builder.RegisterWebObjectActivator();
20+
builder.AddSystemWebDependencyInjection();
2121

2222
builder.AddSystemWebAdapters()
2323
.AddVirtualizedContentDirectories();

samples/AuthRemoteFormsAuth/AuthRemoteFormsAuthFramework/Web.config

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<configSections>
44
<section name="configBuilders" type="System.Configuration.ConfigurationBuildersSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />
@@ -189,4 +189,10 @@
189189
</dependentAssembly>
190190
</assemblyBinding>
191191
</runtime>
192+
<system.codedom>
193+
<compilers>
194+
<compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701;612;618" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
195+
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008,40000,40008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
196+
</compilers>
197+
</system.codedom>
192198
</configuration>

0 commit comments

Comments
 (0)