Files
Telegrator/Telegrator.Hosting/Providers/HostHandlersProvider.cs
T

47 lines
1.8 KiB
C#
Raw Normal View History

2025-07-24 23:19:59 +04:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Telegrator.Handlers.Components;
using Telegrator.MadiatorCore;
using Telegrator.MadiatorCore.Descriptors;
using Telegrator.Providers;
namespace Telegrator.Hosting.Providers
{
/// <inheritdoc/>
2025-07-24 23:19:59 +04:00
public class HostHandlersProvider : HandlersProvider
{
private readonly IServiceProvider Services;
private readonly ILogger<HostHandlersProvider> Logger;
/// <inheritdoc/>
public HostHandlersProvider(
IHandlersCollection handlers,
IOptions<TelegratorOptions> options,
IServiceProvider serviceProvider,
2025-07-27 14:19:40 +04:00
ILogger<HostHandlersProvider> logger) : base(handlers, options.Value)
2025-07-24 23:19:59 +04:00
{
Services = serviceProvider;
Logger = logger;
}
/// <inheritdoc/>
2025-07-24 23:19:59 +04:00
public override UpdateHandlerBase GetHandlerInstance(HandlerDescriptor descriptor, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
IServiceScope scope = Services.CreateScope();
2025-07-27 14:19:40 +04:00
2025-07-24 23:19:59 +04:00
object handlerInstance = descriptor.ServiceKey == null
? scope.ServiceProvider.GetRequiredService(descriptor.HandlerType)
: scope.ServiceProvider.GetRequiredKeyedService(descriptor.HandlerType, descriptor.ServiceKey);
if (handlerInstance is not UpdateHandlerBase updateHandler)
2025-07-27 14:19:40 +04:00
throw new InvalidOperationException("Failed to resolve " + descriptor.HandlerType + " as UpdateHandlerBase");
2025-07-24 23:19:59 +04:00
descriptor.LazyInitialization?.Invoke(updateHandler);
2025-07-24 23:19:59 +04:00
updateHandler.LifetimeToken.OnLifetimeEnded += _ => scope.Dispose();
return updateHandler;
}
}
}