diff --git a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs index 90d10578..1b382abf 100644 --- a/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs +++ b/Hosting/NetCord.Hosting.Services/ApplicationCommands/ApplicationCommandServiceServiceCollectionExtensions.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using NetCord.Hosting.Gateway; @@ -192,6 +193,8 @@ public static IServiceCollection AddHttpApplicationCommands( services.AddShardedGatewayHandler(services => services.GetRequiredService>()); services.AddHttpInteractionHandler(services => services.GetRequiredService>()); + services.TryAddTransient(CreateServiceManager); + services.AddHostedService(); return services; @@ -277,8 +280,23 @@ public static IServiceCollection AddHttpApplicationCommands( services.AddShardedGatewayHandler(services => services.GetRequiredService>()); services.AddHttpInteractionHandler(services => services.GetRequiredService>()); + services.TryAddTransient(CreateServiceManager); + services.AddHostedService(); return services; } + + private static ApplicationCommandServiceManager CreateServiceManager(IServiceProvider services) + { + List managerServices = []; + + foreach (var serviceData in services.GetServices()) + { + serviceData.Builder.Build(); + managerServices.Add(serviceData.Service); + } + + return new(managerServices); + } } diff --git a/Hosting/NetCord.Hosting.Services/ApplicationCommands/IApplicationCommandsBuilder.cs b/Hosting/NetCord.Hosting.Services/ApplicationCommands/IApplicationCommandsBuilder.cs index 58ad3d54..56d2b61d 100644 --- a/Hosting/NetCord.Hosting.Services/ApplicationCommands/IApplicationCommandsBuilder.cs +++ b/Hosting/NetCord.Hosting.Services/ApplicationCommands/IApplicationCommandsBuilder.cs @@ -76,7 +76,7 @@ public EntryPointCommandBuilder AddEntryPointCommand(string name, string descrip public void Build() { - var builders = _builders; + var builders = Interlocked.Exchange(ref _builders, []); int count = builders.Count; for (int i = 0; i < count; i++) @@ -100,8 +100,6 @@ public void Build() break; } } - - _builders = []; } } diff --git a/Hosting/NetCord.Hosting.Services/Commands/ICommandsBuilder.cs b/Hosting/NetCord.Hosting.Services/Commands/ICommandsBuilder.cs index 01343e04..1c1edae5 100644 --- a/Hosting/NetCord.Hosting.Services/Commands/ICommandsBuilder.cs +++ b/Hosting/NetCord.Hosting.Services/Commands/ICommandsBuilder.cs @@ -50,7 +50,7 @@ public CommandGroupBuilder AddCommandGroup(IEnumerable aliases, Action