Skip to content

Commit d4b1953

Browse files
Remove CombinedServiceProvider (#8792)
1 parent 6943bc5 commit d4b1953

File tree

192 files changed

+1342
-1285
lines changed

Some content is hidden

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

192 files changed

+1342
-1285
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore/Extensions/HotChocolateAspNetCoreServiceCollectionExtensions.Http.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(
3131

3232
return builder.ConfigureSchemaServices(s => s
3333
.RemoveAll<IHttpRequestInterceptor>()
34-
.AddSingleton<IHttpRequestInterceptor, T>(sp =>
35-
ActivatorUtilities.GetServiceOrCreateInstance<T>(sp.GetCombinedServices())));
34+
.AddSingleton<IHttpRequestInterceptor, T>());
3635
}
3736

3837
/// <summary>
@@ -60,7 +59,7 @@ public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(
6059

6160
return builder.ConfigureSchemaServices(s => s
6261
.RemoveAll<IHttpRequestInterceptor>()
63-
.AddSingleton<IHttpRequestInterceptor, T>(sp => factory(sp.GetCombinedServices())));
62+
.AddSingleton<IHttpRequestInterceptor, T>(factory));
6463
}
6564

6665
/// <summary>

src/HotChocolate/AspNetCore/src/AspNetCore/Extensions/HotChocolateAspNetCoreServiceCollectionExtensions.Subscriptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
2828
where T : class, ISocketSessionInterceptor =>
2929
builder.ConfigureSchemaServices(s => s
3030
.RemoveAll<ISocketSessionInterceptor>()
31-
.AddSingleton<ISocketSessionInterceptor, T>(
32-
sp => ActivatorUtilities.GetServiceOrCreateInstance<T>(sp.GetCombinedServices())));
31+
.AddSingleton<ISocketSessionInterceptor, T>());
3332

3433
/// <summary>
3534
/// Adds an interceptor for GraphQL socket sessions to the GraphQL configuration.
@@ -52,7 +51,7 @@ public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
5251
where T : class, ISocketSessionInterceptor =>
5352
builder.ConfigureSchemaServices(s => s
5453
.RemoveAll<ISocketSessionInterceptor>()
55-
.AddSingleton<ISocketSessionInterceptor, T>(sp => factory(sp.GetCombinedServices())));
54+
.AddSingleton<ISocketSessionInterceptor, T>(factory));
5655

5756
private static IRequestExecutorBuilder AddSubscriptionServices(
5857
this IRequestExecutorBuilder builder)

src/HotChocolate/Core/src/Execution/DependencyInjection/InternalSchemaServiceCollectionExtensions.cs

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -72,51 +72,4 @@ public static IServiceProvider GetRootServiceProvider(this Schema schema)
7272
/// </returns>
7373
public static IServiceProvider GetRootServiceProvider(this IServiceProvider services)
7474
=> services.GetRequiredService<IRootServiceProviderAccessor>().ServiceProvider;
75-
76-
/// <summary>
77-
/// Gets a service from the root service provider.
78-
/// </summary>
79-
/// <typeparam name="T">
80-
/// The type of the service to get.
81-
/// </typeparam>
82-
/// <param name="services">
83-
/// The schema services.
84-
/// </param>
85-
/// <returns>
86-
/// The service.
87-
/// </returns>
88-
[Obsolete("Use GetRootServiceProvider instead.")]
89-
public static T GetApplicationService<T>(this IServiceProvider services) where T : notnull
90-
=> services.GetApplicationServices().GetRequiredService<T>();
91-
92-
/// <summary>
93-
/// Gets the root service provider from the schema services. This allows
94-
/// schema services to access application level services.
95-
/// </summary>
96-
/// <param name="services">
97-
/// The schema services.
98-
/// </param>
99-
/// <returns>
100-
/// The root service provider.
101-
/// </returns>
102-
[Obsolete("Use GetRootServiceProvider instead.")]
103-
public static IServiceProvider GetApplicationServices(this IServiceProvider services)
104-
=> services.GetRootServiceProvider();
105-
106-
/// <summary>
107-
/// Gets a service provided that represents the combined services from the schema services
108-
/// and application services.
109-
/// </summary>
110-
/// <param name="services">
111-
/// The schema services.
112-
/// </param>
113-
/// <returns>
114-
/// The service.
115-
/// </returns>
116-
public static IServiceProvider GetCombinedServices(this IServiceProvider services)
117-
=> services is CombinedServiceProvider combined
118-
? combined
119-
: new CombinedServiceProvider(
120-
services.GetRootServiceProvider(),
121-
services);
12275
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.ErrorFilter.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public static IRequestExecutorBuilder AddErrorFilter<T>(
3030
ArgumentNullException.ThrowIfNull(factory);
3131

3232
return builder.ConfigureSchemaServices(
33-
s => s.AddSingleton<IErrorFilter, T>(
34-
sp => factory(sp.GetCombinedServices())));
33+
s => s.AddSingleton<IErrorFilter, T>(factory));
3534
}
3635

3736
public static IRequestExecutorBuilder AddErrorFilter<T>(
@@ -42,8 +41,7 @@ public static IRequestExecutorBuilder AddErrorFilter<T>(
4241

4342
builder.Services.TryAddSingleton<T>();
4443
return builder.ConfigureSchemaServices(
45-
s => s.AddSingleton<IErrorFilter, T>(
46-
sp => sp.GetRootServiceProvider().GetRequiredService<T>()));
44+
s => s.AddSingleton<IErrorFilter, T>());
4745
}
4846

4947
public static IServiceCollection AddErrorFilter(

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.Instrumentation.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,37 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
1717

1818
if (typeof(IExecutionDiagnosticEventListener).IsAssignableFrom(typeof(T)))
1919
{
20-
builder.Services.TryAddSingleton<T>();
2120
builder.ConfigureSchemaServices(
22-
s => s.AddSingleton(
23-
sp => (IExecutionDiagnosticEventListener)sp.GetRootServiceProvider().GetRequiredService<T>()));
21+
static s =>
22+
{
23+
s.TryAddSingleton<T>();
24+
s.AddSingleton(static sp => (IExecutionDiagnosticEventListener)sp.GetRequiredService<T>());
25+
});
2426
}
2527
else if (typeof(IDataLoaderDiagnosticEventListener).IsAssignableFrom(typeof(T)))
2628
{
2729
builder.Services.TryAddSingleton<T>();
28-
builder.Services.AddSingleton(s => (IDataLoaderDiagnosticEventListener)s.GetRequiredService<T>());
30+
builder.Services.AddSingleton(
31+
static s => (IDataLoaderDiagnosticEventListener)s.GetRequiredService<T>());
2932
}
3033
else if (typeof(T).IsDefined(typeof(DiagnosticEventSourceAttribute), true))
3134
{
32-
builder.Services.TryAddSingleton<T>();
35+
var attribute = (DiagnosticEventSourceAttribute)typeof(T).GetCustomAttributes(typeof(DiagnosticEventSourceAttribute), true).First();
36+
var listener = attribute.Listener;
3337

34-
builder.ConfigureSchemaServices(static s =>
38+
if (attribute.IsSchemaService)
39+
{
40+
builder.ConfigureSchemaServices(s =>
41+
{
42+
s.TryAddSingleton<T>();
43+
s.AddSingleton(listener, sp => sp.GetRequiredService<T>());
44+
});
45+
}
46+
else
3547
{
36-
var attribute = typeof(T).GetCustomAttributes(typeof(DiagnosticEventSourceAttribute), true).First();
37-
var listener = ((DiagnosticEventSourceAttribute)attribute).Listener;
38-
s.AddSingleton(listener, sp => sp.GetRootServiceProvider().GetRequiredService<T>());
39-
});
48+
builder.Services.TryAddSingleton<T>();
49+
builder.Services.AddSingleton(listener, sp => sp.GetRequiredService<T>());
50+
}
4051
}
4152
else
4253
{
@@ -48,43 +59,34 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
4859

4960
public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
5061
this IRequestExecutorBuilder builder,
51-
Func<IServiceProvider, T> diagnosticEventListener)
62+
Func<IServiceProvider, T> factory)
5263
where T : class
5364
{
5465
ArgumentNullException.ThrowIfNull(builder);
55-
ArgumentNullException.ThrowIfNull(diagnosticEventListener);
66+
ArgumentNullException.ThrowIfNull(factory);
5667

5768
if (typeof(IExecutionDiagnosticEventListener).IsAssignableFrom(typeof(T)))
5869
{
5970
builder.ConfigureSchemaServices(
60-
s => s.AddSingleton(
61-
sp => (IExecutionDiagnosticEventListener)diagnosticEventListener(
62-
sp.GetCombinedServices())));
71+
s => s.AddSingleton(sp => (IExecutionDiagnosticEventListener)factory(sp)));
6372
}
6473
else if (typeof(IDataLoaderDiagnosticEventListener).IsAssignableFrom(typeof(T)))
6574
{
6675
builder.Services.AddSingleton(
67-
s => (IDataLoaderDiagnosticEventListener)diagnosticEventListener(s));
76+
s => (IDataLoaderDiagnosticEventListener)factory(s));
6877
}
6978
else if (typeof(T).IsDefined(typeof(DiagnosticEventSourceAttribute), true))
7079
{
71-
var attribute =
72-
(DiagnosticEventSourceAttribute)typeof(T)
73-
.GetCustomAttributes(typeof(DiagnosticEventSourceAttribute), true)
74-
.First();
80+
var attribute = (DiagnosticEventSourceAttribute)typeof(T).GetCustomAttributes(typeof(DiagnosticEventSourceAttribute), true).First();
81+
var listener = attribute.Listener;
7582

7683
if (attribute.IsSchemaService)
7784
{
78-
builder.ConfigureSchemaServices(s =>
79-
{
80-
var listener = attribute.Listener;
81-
s.AddSingleton(listener, sp => diagnosticEventListener(sp.GetCombinedServices()));
82-
});
85+
builder.ConfigureSchemaServices(s => s.AddSingleton(listener, factory));
8386
}
8487
else
8588
{
86-
var listener = attribute.Listener;
87-
builder.Services.AddSingleton(listener, diagnosticEventListener);
89+
builder.Services.AddSingleton(listener, factory);
8890
}
8991
}
9092
else

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.Optimizer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ public static IRequestExecutorBuilder AddOperationCompilerOptimizer<T>(
2424
ArgumentNullException.ThrowIfNull(builder);
2525

2626
builder.ConfigureSchemaServices(
27-
sc => sc.AddSingleton<IOperationCompilerOptimizer>(
28-
sp => factory(sp.GetCombinedServices())));
27+
sc => sc.AddSingleton<IOperationCompilerOptimizer>(factory));
2928
return builder;
3029
}
3130
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.Services.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,22 @@ public static IRequestExecutorBuilder AddScopedServiceInitializer<TService>(
3333
builder.Services.AddSingleton<IServiceScopeInitializer>(new DelegateServiceInitializer<TService>(initializer));
3434
return builder;
3535
}
36+
37+
/// <summary>
38+
/// Resolves an instance of <typeparamref name="TService"/> from the application
39+
/// service provider and makes it available as a Singleton through the schema
40+
/// service provider.
41+
/// </summary>
42+
/// <typeparam name="TService">
43+
/// The type of service.
44+
/// </typeparam>
45+
public static IRequestExecutorBuilder AddApplicationService<TService>(
46+
this IRequestExecutorBuilder builder)
47+
where TService : class
48+
{
49+
return builder.ConfigureSchemaServices(
50+
static (sp, sc) => sc.AddSingleton(sp.GetRequiredService<TService>()));
51+
}
3652
}
3753

3854
file sealed class DelegateServiceInitializer<TService>(

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.TransactionScope.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(
3333

3434
return ConfigureSchemaServices(
3535
builder,
36-
services =>
36+
static services =>
3737
{
38-
// we remove all handlers from the schema DI
39-
services.RemoveAll(typeof(ITransactionScopeHandler));
40-
41-
// and then reference the transaction scope handler from the global DI.
42-
services.AddSingleton<ITransactionScopeHandler>(
43-
s => s.GetRootServiceProvider().GetRequiredService<T>());
38+
services.RemoveAll<ITransactionScopeHandler>();
39+
services.AddSingleton<ITransactionScopeHandler, T>();
4440
});
4541
}
4642

@@ -50,7 +46,7 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(
5046
/// <param name="builder">
5147
/// The request executor builder.
5248
/// </param>
53-
/// <param name="create">
49+
/// <param name="factory">
5450
/// A factory to create the transaction scope.
5551
/// </param>
5652
/// <returns>
@@ -59,17 +55,17 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(
5955
/// <exception cref="ArgumentNullException"></exception>
6056
public static IRequestExecutorBuilder AddTransactionScopeHandler(
6157
this IRequestExecutorBuilder builder,
62-
Func<IServiceProvider, ITransactionScopeHandler> create)
58+
Func<IServiceProvider, ITransactionScopeHandler> factory)
6359
{
6460
ArgumentNullException.ThrowIfNull(builder);
65-
ArgumentNullException.ThrowIfNull(create);
61+
ArgumentNullException.ThrowIfNull(factory);
6662

6763
return ConfigureSchemaServices(
6864
builder,
6965
services =>
7066
{
71-
services.RemoveAll(typeof(ITransactionScopeHandler));
72-
services.AddSingleton(sp => create(sp.GetCombinedServices()));
67+
services.RemoveAll<ITransactionScopeHandler>();
68+
services.AddSingleton(factory);
7369
});
7470
}
7571

@@ -101,10 +97,7 @@ internal static IRequestExecutorBuilder TryAddNoOpTransactionScopeHandler(
10197

10298
return ConfigureSchemaServices(
10399
builder,
104-
services =>
105-
{
106-
services.TryAddSingleton<ITransactionScopeHandler>(
107-
sp => sp.GetRootServiceProvider().GetRequiredService<NoOpTransactionScopeHandler>());
108-
});
100+
static services =>
101+
services.TryAddSingleton<ITransactionScopeHandler, NoOpTransactionScopeHandler>());
109102
}
110103
}

src/HotChocolate/Core/src/Types/Configuration/TypeRegistrar.CreateInstance.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ public TypeSystemObject CreateInstance(Type namedSchemaType)
99
{
1010
try
1111
{
12-
return (TypeSystemObject)ActivatorUtilities.CreateInstance(_combinedServices, namedSchemaType);
12+
var services = _applicationServices ?? _schemaServices;
13+
14+
return (TypeSystemObject)ActivatorUtilities.CreateInstance(services, namedSchemaType);
1315
}
1416
catch (Exception ex)
1517
{

src/HotChocolate/Core/src/Types/Configuration/TypeRegistrar.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using HotChocolate.Internal;
22
using HotChocolate.Types;
33
using HotChocolate.Types.Descriptors;
4-
using HotChocolate.Utilities;
54

65
namespace HotChocolate.Configuration;
76

@@ -15,7 +14,6 @@ internal sealed partial class TypeRegistrar : ITypeRegistrar
1514
private readonly TypeInterceptor _interceptor;
1615
private readonly IServiceProvider _schemaServices;
1716
private readonly IServiceProvider? _applicationServices;
18-
private readonly IServiceProvider _combinedServices;
1917

2018
public TypeRegistrar(IDescriptorContext context,
2119
TypeRegistry typeRegistry,
@@ -32,10 +30,6 @@ public TypeRegistrar(IDescriptorContext context,
3230
throw new ArgumentNullException(nameof(typeInterceptor));
3331
_schemaServices = context.Services;
3432
_applicationServices = context.Services.GetService<IRootServiceProviderAccessor>()?.ServiceProvider;
35-
36-
_combinedServices = _applicationServices is null
37-
? _schemaServices
38-
: new CombinedServiceProvider(_schemaServices, _applicationServices);
3933
}
4034

4135
public ISet<string> Scalars { get; } = new HashSet<string>();

0 commit comments

Comments
 (0)