From 7fb537fd72c9853d27f234e23995da55ab3460b0 Mon Sep 17 00:00:00 2001 From: mini <39670899+minisbett@users.noreply.github.com> Date: Sat, 1 Mar 2025 21:52:41 +0100 Subject: [PATCH] [Featured] Consider `AutoServiceScopes` in autocomplete handler executions (#3068) * Add AutoServiceScopes to autocomplete handlers * Fix disposing behavior * Update AutocompleteHandler.cs --------- Co-authored-by: Mihail Gribkov <61027276+Misha-133@users.noreply.github.com> --- .../AutocompleteHandlers/AutocompleteHandler.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs b/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs index 636ac840..a41298fc 100644 --- a/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs +++ b/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs @@ -4,6 +4,7 @@ using System; using System.Reflection; using System.Runtime.ExceptionServices; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; namespace Discord.Interactions { @@ -29,6 +30,9 @@ namespace Discord.Interactions public Task ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter, IServiceProvider services) { + using IServiceScope scope = InteractionService._autoServiceScopes ? services?.CreateScope() : null; + services = InteractionService._autoServiceScopes ? scope?.ServiceProvider ?? EmptyServiceProvider.Instance : services; + switch (InteractionService._runMode) { case RunMode.Sync: @@ -44,7 +48,7 @@ namespace Discord.Interactions default: throw new InvalidOperationException($"RunMode {InteractionService._runMode} is not supported."); } - + return Task.FromResult((IResult)ExecuteResult.FromSuccess()); }