Snowflake/RPG.Services.Core/Extensions/HostApplicationBuilderExtensions.cs

30 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 HostApplicationBuilder SetupRPGService<TService, TCommandHandler>(this HostApplicationBuilder builder)
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<SessionManager>()
.AddSingleton<ServiceBox>()
.AddSingleton<ServiceCommandHandler, TCommandHandler>();
return builder;
}
}