Skip to content

Commit 7fb537f

Browse files
minisbettMisha-133
andauthored
[Featured] Consider AutoServiceScopes in autocomplete handler executions (#3068)
* Add AutoServiceScopes to autocomplete handlers * Fix disposing behavior * Update AutocompleteHandler.cs --------- Co-authored-by: Mihail Gribkov <[email protected]>
1 parent 5f6c26b commit 7fb537f

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reflection;
55
using System.Runtime.ExceptionServices;
66
using System.Threading.Tasks;
7+
using Microsoft.Extensions.DependencyInjection;
78

89
namespace Discord.Interactions
910
{
@@ -29,6 +30,9 @@ protected virtual string GetLogString(IInteractionContext context)
2930
public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInteraction autocompleteInteraction, IParameterInfo parameter,
3031
IServiceProvider services)
3132
{
33+
using IServiceScope scope = InteractionService._autoServiceScopes ? services?.CreateScope() : null;
34+
services = InteractionService._autoServiceScopes ? scope?.ServiceProvider ?? EmptyServiceProvider.Instance : services;
35+
3236
switch (InteractionService._runMode)
3337
{
3438
case RunMode.Sync:
@@ -44,7 +48,7 @@ public Task<IResult> ExecuteAsync(IInteractionContext context, IAutocompleteInte
4448
default:
4549
throw new InvalidOperationException($"RunMode {InteractionService._runMode} is not supported.");
4650
}
47-
51+
4852
return Task.FromResult((IResult)ExecuteResult.FromSuccess());
4953
}
5054

0 commit comments

Comments
 (0)