Fix size regression in DependencyInjection (#56843)
authorEric Erhardt <eric.erhardt@microsoft.com>
Wed, 4 Aug 2021 20:06:41 +0000 (15:06 -0500)
committerGitHub <noreply@github.com>
Wed, 4 Aug 2021 20:06:41 +0000 (15:06 -0500)
With the new 6.0 TFM, we are no longer checking IsDynamicCodeCompiled to fall back to Reflection on Blazor WASM. Thus we are now using IL Ref Emit on WASM.

The issue was the #ifs were no longer correct for the new TFM. I've made the #ifs consistent across DI and DI.Abstractions such that the #ifs are for old TFMs and any new TFM will use the #else block.

Fix #56779

src/libraries/Microsoft.Extensions.DependencyInjection.Abstractions/src/ActivatorUtilities.cs
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/CallSiteRuntimeResolver.cs
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/Expressions/ExpressionResolverBuilder.cs
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/ILEmit/ILEmitResolverBuilder.cs
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/StackGuard.cs
src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceProvider.cs

index 99ef9ae..8151656 100644 (file)
@@ -396,9 +396,7 @@ namespace Microsoft.Extensions.DependencyInjection
                     }
                 }
 
-#if NETCOREAPP
-                return _constructor.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: _parameterValues, culture: null);
-#else
+#if NETFRAMEWORK || NETSTANDARD2_0
                 try
                 {
                     return _constructor.Invoke(_parameterValues);
@@ -409,6 +407,8 @@ namespace Microsoft.Extensions.DependencyInjection
                     // The above line will always throw, but the compiler requires we throw explicitly.
                     throw;
                 }
+#else
+                return _constructor.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: _parameterValues, culture: null);
 #endif
             }
         }
index fabaea9..1e33062 100644 (file)
@@ -53,9 +53,7 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
                 }
             }
 
-#if NETSTANDARD2_1
-            return constructorCallSite.ConstructorInfo.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: parameterValues, culture: null);
-#else
+#if NETFRAMEWORK || NETSTANDARD2_0
             try
             {
                 return constructorCallSite.ConstructorInfo.Invoke(parameterValues);
@@ -66,6 +64,8 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
                 // The above line will always throw, but the compiler requires we throw explicitly.
                 throw;
             }
+#else
+            return constructorCallSite.ConstructorInfo.Invoke(BindingFlags.DoNotWrapExceptions, binder: null, parameters: parameterValues, culture: null);
 #endif
         }
 
index 4fbed79..a74a004 100644 (file)
@@ -55,10 +55,10 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
             // Only scope methods are cached
             if (callSite.Cache.Location == CallSiteResultCacheLocation.Scope)
             {
-#if NETSTANDARD2_1
-                return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, _buildTypeDelegate, callSite);
-#else
+#if NETFRAMEWORK || NETSTANDARD2_0
                 return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, key => _buildTypeDelegate(key, callSite));
+#else
+                return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, _buildTypeDelegate, callSite);
 #endif
             }
 
index 6cc6ba3..aeb27d9 100644 (file)
@@ -69,10 +69,10 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
             // Only scope methods are cached
             if (callSite.Cache.Location == CallSiteResultCacheLocation.Scope)
             {
-#if NETSTANDARD2_1
-                return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, _buildTypeDelegate, callSite);
-#else
+#if NETFRAMEWORK || NETSTANDARD2_0
                 return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, key => _buildTypeDelegate(key, callSite));
+#else
+                return _scopeResolverCache.GetOrAdd(callSite.Cache.Key, _buildTypeDelegate, callSite);
 #endif
             }
 
index 8900487..a11c361 100644 (file)
@@ -16,12 +16,7 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
 
         public bool TryEnterOnCurrentStack()
         {
-#if NETSTANDARD2_1
-            if (RuntimeHelpers.TryEnsureSufficientExecutionStack())
-            {
-                return true;
-            }
-#else
+#if NETFRAMEWORK || NETSTANDARD2_0
             try
             {
                 RuntimeHelpers.EnsureSufficientExecutionStack();
@@ -30,6 +25,11 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
             catch (InsufficientExecutionStackException)
             {
             }
+#else
+            if (RuntimeHelpers.TryEnsureSufficientExecutionStack())
+            {
+                return true;
+            }
 #endif
 
             if (_executionStackCount < MaxExecutionStackCount)
@@ -42,19 +42,19 @@ namespace Microsoft.Extensions.DependencyInjection.ServiceLookup
 
         public TR RunOnEmptyStack<T1, T2, TR>(Func<T1, T2, TR> action, T1 arg1, T2 arg2)
         {
-            // Prefer ValueTuple when available to reduce dependencies on Tuple
-#if NETSTANDARD2_1
+#if NETFRAMEWORK || NETSTANDARD2_0
             return RunOnEmptyStackCore(static s =>
             {
-                var t = ((Func<T1, T2, TR>, T1, T2))s;
+                var t = (Tuple<Func<T1, T2, TR>, T1, T2>)s;
                 return t.Item1(t.Item2, t.Item3);
-            }, (action, arg1, arg2));
+            }, Tuple.Create(action, arg1, arg2));
 #else
+            // Prefer ValueTuple when available to reduce dependencies on Tuple
             return RunOnEmptyStackCore(static s =>
             {
-                var t = (Tuple<Func<T1, T2, TR>, T1, T2>)s;
+                var t = ((Func<T1, T2, TR>, T1, T2))s;
                 return t.Item1(t.Item2, t.Item3);
-            }, Tuple.Create(action, arg1, arg2));
+            }, (action, arg1, arg2));
 #endif
         }
 
index a05e82f..cff40d1 100644 (file)
@@ -184,7 +184,7 @@ namespace Microsoft.Extensions.DependencyInjection
         {
             ServiceProviderEngine engine;
 
-#if !NETSTANDARD2_1
+#if NETFRAMEWORK || NETSTANDARD2_0
             engine = new DynamicServiceProviderEngine(this);
 #else
             if (RuntimeFeature.IsDynamicCodeCompiled)