diff --git a/src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs b/src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs index 36cc6cbd8..a72a28bbd 100644 --- a/src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs +++ b/src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs @@ -131,14 +131,24 @@ namespace Discord.Interactions { case RunMode.Sync: { - using var scope = services?.CreateScope(); - return await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false); + if (CommandService._autoServiceScopes) + { + using var scope = services?.CreateScope(); + return await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false); + } + else + return await ExecuteInternalAsync(context, args, services).ConfigureAwait(false); } case RunMode.Async: _ = Task.Run(async () => { - using var scope = services?.CreateScope(); - await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false); + if (CommandService._autoServiceScopes) + { + using var scope = services?.CreateScope(); + await ExecuteInternalAsync(context, args, scope?.ServiceProvider ?? EmptyServiceProvider.Instance).ConfigureAwait(false); + } + else + await ExecuteInternalAsync(context, args, services).ConfigureAwait(false); }); break; default: diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index d7192129d..a9d8ec16c 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -67,7 +67,7 @@ namespace Discord.Interactions internal readonly LogManager _logManager; internal readonly Func _getRestClient; - internal readonly bool _throwOnError, _useCompiledLambda, _enableAutocompleteHandlers; + internal readonly bool _throwOnError, _useCompiledLambda, _enableAutocompleteHandlers, _autoServiceScopes; internal readonly string _wildCardExp; internal readonly RunMode _runMode; internal readonly RestResponseCallback _restResponseCallback; @@ -156,6 +156,7 @@ namespace Discord.Interactions _wildCardExp = config.WildCardExpression; _useCompiledLambda = config.UseCompiledLambda; _enableAutocompleteHandlers = config.EnableAutocompleteHandlers; + _autoServiceScopes = config.AutoServiceScopes; _restResponseCallback = config.RestResponseCallback; _genericTypeConverters = new ConcurrentDictionary diff --git a/src/Discord.Net.Interactions/InteractionServiceConfig.cs b/src/Discord.Net.Interactions/InteractionServiceConfig.cs index e5ca5b9ec..a1583a124 100644 --- a/src/Discord.Net.Interactions/InteractionServiceConfig.cs +++ b/src/Discord.Net.Interactions/InteractionServiceConfig.cs @@ -47,6 +47,11 @@ namespace Discord.Interactions /// public bool EnableAutocompleteHandlers { get; set; } = true; + /// + /// Gets or sets whether new service scopes should be automatically created when resolving module depedencies on every command execution. + /// + public bool AutoServiceScopes { get; set; } = true; + /// /// Gets or sets delegate to be used by the when responding to a Rest based interaction. ///