[Fix] Interaction Service failing to create scopes with some DI providers (#3031)

This commit is contained in:
Mihail Gribkov
2024-12-01 20:32:26 +03:00
committed by GitHub
parent 96a832788c
commit a884ad18dd
2 changed files with 13 additions and 3 deletions

View File

@@ -462,9 +462,12 @@ namespace Discord.Interactions.Builders
{
if (TypeInfo is not null && ModuleClassBuilder.IsValidModuleDefinition(TypeInfo))
{
using var scope = services?.CreateScope();
IServiceScope scope = null;
if (interactionService._autoServiceScopes)
{
scope = services?.CreateScope();
services = scope?.ServiceProvider ?? EmptyServiceProvider.Instance;
}
var instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
@@ -473,10 +476,13 @@ namespace Discord.Interactions.Builders
instance.Construct(this, interactionService);
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
instance.OnModuleBuilding(interactionService, moduleInfo);
scope?.Dispose();
return moduleInfo;
}
finally
{
scope?.Dispose();
(instance as IDisposable)?.Dispose();
}
}

View File

@@ -113,10 +113,12 @@ namespace Discord.Interactions
{
await CommandService._cmdLogger.DebugAsync($"Executing {GetLogString(context)}").ConfigureAwait(false);
using var scope = services?.CreateScope();
IServiceScope scope = null;
if (CommandService._autoServiceScopes)
{
scope = services?.CreateScope();
services = scope?.ServiceProvider ?? EmptyServiceProvider.Instance;
}
try
{
@@ -179,11 +181,13 @@ namespace Discord.Interactions
ExceptionDispatchInfo.Capture(ex).Throw();
}
scope?.Dispose();
return result;
}
finally
{
await CommandService._cmdLogger.VerboseAsync($"Executed {GetLogString(context)}").ConfigureAwait(false);
scope?.Dispose();
}
}