From 1189bee873b38ac114507aa291fa06a68d8709ae Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Mon, 10 Nov 2025 11:04:13 -0800 Subject: [PATCH] Add msbuild property to disable di generation This is important for scenarios where there are internalsvisibleto and so we don't need multiple declarations of it. --- .../FrameworkDependencyInjectionGenerator.cs | 15 ++++++++++++++- .../build/DependencyInjectionGenerator.targets | 8 ++++++++ ...Microsoft.AspNetCore.SystemWebAdapters.targets | 1 + 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/Microsoft.AspNetCore.SystemWebAdapters/build/DependencyInjectionGenerator.targets diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp/FrameworkDependencyInjectionGenerator.cs b/src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp/FrameworkDependencyInjectionGenerator.cs index 8e5e15350b..2e0251192c 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp/FrameworkDependencyInjectionGenerator.cs +++ b/src/Microsoft.AspNetCore.SystemWebAdapters.Analyzers.CSharp/FrameworkDependencyInjectionGenerator.cs @@ -1,3 +1,4 @@ +using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.IO; @@ -29,6 +30,10 @@ public class FrameworkDependencyInjectionGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { + var options = context.AnalyzerConfigOptionsProvider + .Select((c, _) => + c.GlobalOptions.TryGetValue("build_property.EnableSystemWebDependencyInjectionGenerator", out var enableSwitch) && enableSwitch.Equals("true", StringComparison.OrdinalIgnoreCase)); + var usedFrameworks = context.CompilationProvider.Select((compilation, token) => new FrameworksUsed { HttpApplicationHost = compilation.GetTypeByMetadataName("Microsoft.AspNetCore.SystemWebAdapters.Hosting.IDependencyRegistrar") is { }, @@ -36,8 +41,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context) Mvc = compilation.GetTypeByMetadataName("System.Web.Mvc.IDependencyResolver") is { }, }); - context.RegisterSourceOutput(usedFrameworks, (context, frameworks) => + context.RegisterSourceOutput(usedFrameworks.Combine(options), (context, repoContext) => { + var frameworks = repoContext.Left; + var isEnabled = repoContext.Right; + + if (!isEnabled) + { + return; + } + // No need to do anything if they're not referencing SystemWebAdapters with the ASP.NET Framework hosting infrastructure if (!frameworks.HttpApplicationHost) { diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/build/DependencyInjectionGenerator.targets b/src/Microsoft.AspNetCore.SystemWebAdapters/build/DependencyInjectionGenerator.targets new file mode 100644 index 0000000000..df88f01faf --- /dev/null +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/build/DependencyInjectionGenerator.targets @@ -0,0 +1,8 @@ + + + true + + + + + diff --git a/src/Microsoft.AspNetCore.SystemWebAdapters/build/Microsoft.AspNetCore.SystemWebAdapters.targets b/src/Microsoft.AspNetCore.SystemWebAdapters/build/Microsoft.AspNetCore.SystemWebAdapters.targets index 4ebbca1fce..5ef1b80525 100644 --- a/src/Microsoft.AspNetCore.SystemWebAdapters/build/Microsoft.AspNetCore.SystemWebAdapters.targets +++ b/src/Microsoft.AspNetCore.SystemWebAdapters/build/Microsoft.AspNetCore.SystemWebAdapters.targets @@ -1,3 +1,4 @@ +