KafkaSR/RPG.Services.Core/Extensions/HostApplicationBuilderExtensions.cs
2024-01-30 02:49:21 +03:00

31 lines
1.3 KiB
C#

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using RPG.Services.Core.Network;
using RPG.Services.Core.Network.Command;
using RPG.Services.Core.Options;
using RPG.Services.Core.Session;
namespace RPG.Services.Core.Extensions;
public static class HostApplicationBuilderExtensions
{
public static IHostApplicationBuilder SetupRPGService<TService, TCommandHandler>(this IHostApplicationBuilder builder, bool stateless = false)
where TService : RPGServiceBase
where TCommandHandler : ServiceCommandHandler
{
IConfigurationSection serviceOptionsSection = builder.Configuration.GetRequiredSection("Service");
IConfigurationSection serviceNodesSection = builder.Configuration.GetRequiredSection("Nodes");
builder.Services.Configure<RPGServiceOptions>(serviceOptionsSection)
.Configure<ServiceNodeOptions>(serviceNodesSection);
builder.Services.AddHostedService<TService>()
.AddSingleton<ServiceManager>()
.AddSingleton<ServiceBox>()
.AddSingleton<ServiceCommandHandler, TCommandHandler>();
if (!stateless) builder.Services.AddSingleton<SessionManager>();
return builder;
}
}