@@ -14,6 +14,7 @@ namespace Microsoft.Extensions.DependencyInjection;
1414using Microsoft . AspNetCore . Mvc . Routing ;
1515using Microsoft . Extensions . DependencyInjection . Extensions ;
1616using Microsoft . Extensions . Options ;
17+ using System . Runtime . CompilerServices ;
1718using static Microsoft . Extensions . DependencyInjection . ServiceDescriptor ;
1819
1920/// <summary>
@@ -83,19 +84,25 @@ private static object CreateInstance( this IServiceProvider services, ServiceDes
8384 return ActivatorUtilities . GetServiceOrCreateInstance ( services , descriptor . ImplementationType ! ) ;
8485 }
8586
86- private static ServiceDescriptor WithUrlHelperFactoryDecorator ( IServiceCollection services )
87+ [ SkipLocalsInit ]
88+ private static DecoratedServiceDescriptor WithUrlHelperFactoryDecorator ( IServiceCollection services )
8789 {
8890 var descriptor = services . FirstOrDefault ( sd => sd . ServiceType == typeof ( IUrlHelperFactory ) ) ;
8991
90- if ( descriptor is DecoratedServiceDescriptor )
92+ if ( descriptor is DecoratedServiceDescriptor sd )
9193 {
92- return descriptor ;
94+ return sd ;
9395 }
9496
95- var lifetime = ServiceLifetime . Singleton ;
96- Func < IServiceProvider , object > instantiate = sp => new UrlHelperFactory ( ) ;
97+ ServiceLifetime lifetime ;
98+ Func < IServiceProvider , object > instantiate ;
9799
98- if ( descriptor != null )
100+ if ( descriptor == null )
101+ {
102+ lifetime = ServiceLifetime . Singleton ;
103+ instantiate = static sp => new UrlHelperFactory ( ) ;
104+ }
105+ else
99106 {
100107 lifetime = descriptor . Lifetime ;
101108 instantiate = sp => sp . CreateInstance ( descriptor ) ;
@@ -116,7 +123,7 @@ IUrlHelperFactory NewFactory( IServiceProvider serviceProvider )
116123 return ( IUrlHelperFactory ) instance ;
117124 }
118125
119- return new DecoratedServiceDescriptor ( typeof ( IUrlHelperFactory ) , NewFactory , lifetime ) ;
126+ return new ( typeof ( IUrlHelperFactory ) , NewFactory , lifetime ) ;
120127 }
121128
122129 private sealed class DecoratedServiceDescriptor : ServiceDescriptor
0 commit comments