Skip to content

Commit

Permalink
[Fix] Interaction Service failing to create scopes with some DI provi…
Browse files Browse the repository at this point in the history
…ders (#3031)
  • Loading branch information
Misha-133 authored Dec 1, 2024
1 parent 96a8327 commit a884ad1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Discord.Net.Interactions/Builders/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,12 @@ internal ModuleInfo Build(InteractionService interactionService, IServiceProvide
{
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);

Expand All @@ -473,10 +476,13 @@ internal ModuleInfo Build(InteractionService interactionService, IServiceProvide
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();
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Discord.Net.Interactions/Info/Commands/CommandInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ private async Task<IResult> ExecuteInternalAsync(IInteractionContext context, IS
{
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
{
Expand Down Expand Up @@ -179,11 +181,13 @@ private async Task<IResult> ExecuteInternalAsync(IInteractionContext context, IS
ExceptionDispatchInfo.Capture(ex).Throw();
}

scope?.Dispose();
return result;
}
finally
{
await CommandService._cmdLogger.VerboseAsync($"Executed {GetLogString(context)}").ConfigureAwait(false);
scope?.Dispose();
}
}

Expand Down

0 comments on commit a884ad1

Please sign in to comment.