Browse Source

Make module service scopes optional (#1997)

Co-Authored-By: Cenk Ergen <57065323+Cenngo@users.noreply.github.com>
tags/3.1.0
Quin Lynch GitHub 3 years ago
parent
commit
cb1aad308b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 5 deletions
  1. +14
    -4
      src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs
  2. +2
    -1
      src/Discord.Net.Interactions/InteractionService.cs
  3. +5
    -0
      src/Discord.Net.Interactions/InteractionServiceConfig.cs

+ 14
- 4
src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs View File

@@ -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:


+ 2
- 1
src/Discord.Net.Interactions/InteractionService.cs View File

@@ -67,7 +67,7 @@ namespace Discord.Interactions
internal readonly LogManager _logManager;
internal readonly Func<DiscordRestClient> _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<Type, Type>


+ 5
- 0
src/Discord.Net.Interactions/InteractionServiceConfig.cs View File

@@ -47,6 +47,11 @@ namespace Discord.Interactions
/// </remarks>
public bool EnableAutocompleteHandlers { get; set; } = true;

/// <summary>
/// Gets or sets whether new service scopes should be automatically created when resolving module depedencies on every command execution.
/// </summary>
public bool AutoServiceScopes { get; set; } = true;

/// <summary>
/// Gets or sets delegate to be used by the <see cref="InteractionService"/> when responding to a Rest based interaction.
/// </summary>


Loading…
Cancel
Save