-
-
Notifications
You must be signed in to change notification settings - Fork 803
Move GetRootServiceProvider to abstractions #9067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| using HotChocolate; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| public static class ExecutionServiceProviderExtensions | ||
| { | ||
| /// <summary> | ||
| /// Gets the root service provider from the schema services. This allows | ||
| /// schema services to access application level services. | ||
| /// </summary> | ||
| /// <param name="schema"> | ||
| /// The schema. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The root service provider. | ||
| /// </returns> | ||
| public static IServiceProvider GetRootServiceProvider(this ISchemaDefinition schema) | ||
| => schema.Services.GetRequiredService<IRootServiceProviderAccessor>().ServiceProvider; | ||
|
|
||
| /// <summary> | ||
| /// Gets the root service provider from the schema services. This allows | ||
| /// schema services to access application level services. | ||
| /// </summary> | ||
| /// <param name="services"> | ||
| /// The schema services. | ||
| /// </param> | ||
| /// <returns> | ||
| /// The root service provider. | ||
| /// </returns> | ||
| public static IServiceProvider GetRootServiceProvider(this IServiceProvider services) | ||
| => services.GetRequiredService<IRootServiceProviderAccessor>().ServiceProvider; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| using HotChocolate.Execution; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| using HotChocolate.Fusion.Configuration; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Microsoft.Extensions.DependencyInjection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -19,4 +20,14 @@ public static IFusionGatewayBuilder AddApplicationService<TService>( | |||||||||||||||||||||||||||||||||||||||||||||||||||
| return builder.ConfigureSchemaServices( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| static (sp, sc) => sc.AddSingleton(sp.GetRequiredService<TService>())); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static ValueTask<IRequestExecutor> BuildRequestExecutorAsync( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| this IFusionGatewayBuilder builder, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| string? schemaName = null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| CancellationToken cancellationToken = default) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .Services | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .BuildServiceProvider() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .GetRequiredService<IRequestExecutorProvider>() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| .GetExecutorAsync(schemaName, cancellationToken); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+32
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| .GetRequiredService<IRequestExecutorProvider>() | |
| .GetExecutorAsync(schemaName, cancellationToken); | |
| .BuildRequestExecutorAsync(schemaName, cancellationToken); |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling BuildServiceProvider() creates a new service provider instance that is not disposed. This can lead to resource leaks, especially if the service provider contains disposable services. The existing implementation in RequestExecutorServiceProviderExtensions.cs has the same issue, but adding another instance of this pattern compounds the problem. Consider documenting this limitation or using a using statement to ensure proper disposal.
| public static ValueTask<IRequestExecutor> BuildRequestExecutorAsync( | |
| this IFusionGatewayBuilder builder, | |
| string? schemaName = null, | |
| CancellationToken cancellationToken = default) => | |
| builder | |
| .Services | |
| .BuildServiceProvider() | |
| .GetRequiredService<IRequestExecutorProvider>() | |
| .GetExecutorAsync(schemaName, cancellationToken); | |
| public static async ValueTask<IRequestExecutor> BuildRequestExecutorAsync( | |
| this IFusionGatewayBuilder builder, | |
| string? schemaName = null, | |
| CancellationToken cancellationToken = default) | |
| { | |
| using var serviceProvider = builder.Services.BuildServiceProvider(); | |
| var executorProvider = serviceProvider.GetRequiredService<IRequestExecutorProvider>(); | |
| return await executorProvider | |
| .GetExecutorAsync(schemaName, cancellationToken) | |
| .ConfigureAwait(false); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is missing XML documentation comments. Based on the existing pattern in the codebase (see
RequestExecutorServiceProviderExtensions.cslines 93-109), this method should include XML documentation describing its purpose, parameters, and return value.