Add empty service scopes (#3078)
This commit is contained in:
@@ -467,12 +467,13 @@ namespace Discord.Interactions.Builders
|
||||
{
|
||||
if (TypeInfo is not null && ModuleClassBuilder.IsValidModuleDefinition(TypeInfo))
|
||||
{
|
||||
IServiceScope scope = null;
|
||||
if (interactionService._autoServiceScopes)
|
||||
{
|
||||
scope = services?.CreateScope();
|
||||
services = scope?.ServiceProvider ?? EmptyServiceProvider.Instance;
|
||||
}
|
||||
using var scope = InteractionService._autoServiceScopes
|
||||
? services.CreateScope()
|
||||
: null;
|
||||
|
||||
services = (InteractionService._autoServiceScopes
|
||||
? scope?.ServiceProvider
|
||||
: services) ?? EmptyServiceProvider.Instance;
|
||||
|
||||
var instance = ReflectionUtils<IInteractionModuleBase>.CreateObject(TypeInfo, interactionService, services);
|
||||
|
||||
@@ -482,12 +483,10 @@ namespace Discord.Interactions.Builders
|
||||
var moduleInfo = new ModuleInfo(this, interactionService, services, parent);
|
||||
instance.OnModuleBuilding(interactionService, moduleInfo);
|
||||
|
||||
scope?.Dispose();
|
||||
return moduleInfo;
|
||||
}
|
||||
finally
|
||||
{
|
||||
scope?.Dispose();
|
||||
(instance as IDisposable)?.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,31 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace Discord.Interactions
|
||||
{
|
||||
internal class EmptyServiceProvider : IServiceProvider
|
||||
{
|
||||
public static EmptyServiceProvider Instance => new EmptyServiceProvider();
|
||||
namespace Discord.Interactions;
|
||||
|
||||
public object GetService(Type serviceType) => null;
|
||||
internal class EmptyServiceProvider : IServiceProvider
|
||||
{
|
||||
public static EmptyServiceProvider Instance => new ();
|
||||
|
||||
public object GetService(Type serviceType)
|
||||
{
|
||||
if (serviceType == typeof(IServiceScopeFactory))
|
||||
return EmptyServiceScopeFactory.Instance;
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
internal class EmptyServiceScopeFactory : IServiceScopeFactory
|
||||
{
|
||||
public static EmptyServiceScopeFactory Instance => new ();
|
||||
|
||||
public IServiceScope CreateScope() => new EmptyServiceScope();
|
||||
}
|
||||
|
||||
internal class EmptyServiceScope : IServiceScope
|
||||
{
|
||||
public IServiceProvider ServiceProvider => EmptyServiceProvider.Instance;
|
||||
|
||||
public void Dispose() { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user