From 233a67f2b0e4c713d91bc697d0d61ad682084f70 Mon Sep 17 00:00:00 2001 From: Rikitav Date: Fri, 3 Apr 2026 19:23:53 +0400 Subject: [PATCH] * Added RemapWebhook method to WebHost * Updatetd dependencues * Fixed some warnings --- docs/Telegrator.Hosting.Web.xml | 59 ++++++++++--------- docs/Telegrator.Hosting.xml | 44 ++++++-------- .../Telegartor.RedisStateStorage.csproj | 2 +- .../Hosting.Web/TelegramBotWebHost.cs | 12 ++-- .../Hosting.Web/TelegramBotWebHostBuilder.cs | 39 ++---------- .../Mediation/HostedUpdateWebhooker.cs | 48 +++++++++++---- .../Telegrator.Hosting.Web.csproj | 2 +- src/Telegrator.Hosting.Web/TypesExtensions.cs | 55 ++++++++++++----- .../Hosting/ITelegramBotHostBuilder.cs | 6 +- .../Hosting/TelegramBotHost.cs | 10 ++-- .../Hosting/TelegramBotHostBuilder.cs | 39 ++---------- .../Telegrator.Hosting.csproj | 6 +- src/Telegrator.Hosting/TypesExtensions.cs | 39 ++++++++---- src/Telegrator/Telegrator.csproj | 6 +- 14 files changed, 184 insertions(+), 183 deletions(-) diff --git a/docs/Telegrator.Hosting.Web.xml b/docs/Telegrator.Hosting.Web.xml index 36a48b9..a4e606a 100644 --- a/docs/Telegrator.Hosting.Web.xml +++ b/docs/Telegrator.Hosting.Web.xml @@ -84,9 +84,7 @@ - - Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. - + @@ -109,37 +107,18 @@ - + Initializes a new instance of the class. - - - - Initializes a new instance of the class. - - - - - - + Initializes a new instance of the class. - - - - - Initializes a new instance of the class. - - - - - @@ -188,7 +167,6 @@ Initiallizes new instance of - @@ -200,6 +178,16 @@ + + + Allows to remap receiving webhook endpoint and map new route to webhost. + + + + + + + Maps bot webhook to application builder @@ -230,17 +218,34 @@ Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. - + Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. - + + + Searchs for hosted service inside hosts services + + + + + + Replaces the initialization logic from TelegramBotWebHost constructor. Initializes the bot and logs handlers on application startup. + + + Allows to remap receiving webhook endpoint and map new route to webhost. + + + + + + Registers service with to receive updates using webhook diff --git a/docs/Telegrator.Hosting.xml b/docs/Telegrator.Hosting.xml index d9a9352..e66dabb 100644 --- a/docs/Telegrator.Hosting.xml +++ b/docs/Telegrator.Hosting.xml @@ -35,6 +35,11 @@ Provides access to configuration of this Hosted telegram bot + + + Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. + + Represents a hosted telegram bot @@ -93,9 +98,7 @@ - - Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. - + @@ -118,37 +121,18 @@ - + Initializes a new instance of the class. - - - - Initializes a new instance of the class. - - - - - - + Initializes a new instance of the class. - - - - - Initializes a new instance of the class. - - - - - @@ -268,7 +252,17 @@ Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. - + + + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + + + + + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + + + Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. diff --git a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj index 67db6f8..bff2542 100644 --- a/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj +++ b/src/Telegartor.RedisStateStorage/Telegartor.RedisStateStorage.csproj @@ -31,7 +31,7 @@ - + diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs index 13eab68..38b96c4 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs +++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHost.cs @@ -65,11 +65,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil /// Creates new with default services and webhook update receiving scheme /// /// - public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions settings) + public static TelegramBotWebHostBuilder CreateBuilder(WebApplicationOptions? settings) { ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings); - TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); + TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp); builder.AddTelegratorWeb(); return builder; } @@ -78,11 +78,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil /// Creates new SLIM with default services and webhook update receiving scheme /// /// - public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions settings) + public static TelegramBotWebHostBuilder CreateSlimBuilder(WebApplicationOptions? settings) { ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings); - TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); + TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp); builder.AddTelegratorWeb(); return builder; } @@ -91,11 +91,11 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil /// Creates new EMPTY WITHOUT any services or update receiving schemes /// /// - public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions settings) + public static TelegramBotWebHostBuilder CreateEmptyBuilder(WebApplicationOptions? settings) { ArgumentNullException.ThrowIfNull(settings, nameof(settings)); WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings); - TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); + TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp); builder.AddTelegratorWeb(); return builder; } diff --git a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs index 97f5b9b..fb813ba 100644 --- a/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs +++ b/src/Telegrator.Hosting.Web/Hosting.Web/TelegramBotWebHostBuilder.cs @@ -9,13 +9,10 @@ using Telegrator.Core; #pragma warning disable IDE0001 namespace Telegrator.Hosting.Web; -/// -/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. -/// +/// public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder { private readonly WebApplicationBuilder _innerBuilder; - private readonly WebApplicationOptions _settings; internal IHandlersCollection _handlers = null!; /// @@ -43,23 +40,9 @@ public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder /// Initializes a new instance of the class. /// /// - /// - public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, WebApplicationOptions? settings = null) + public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder) { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); - _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// - public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, TelegratorOptions? options, WebApplicationOptions? settings) - { - _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); - _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } /// @@ -67,24 +50,10 @@ public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder /// /// /// - /// - public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, WebApplicationOptions settings) + public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers) { _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); - _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// - /// - public TelegramBotWebHostBuilder(WebApplicationBuilder webApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, WebApplicationOptions settings) - { - _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); - _settings = settings ?? throw new ArgumentNullException(nameof(settings)); + _handlers = handlers ?? throw new ArgumentNullException(nameof(handlers)); } /// diff --git a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs index dca6cbf..938b445 100644 --- a/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs +++ b/src/Telegrator.Hosting.Web/Mediation/HostedUpdateWebhooker.cs @@ -26,7 +26,6 @@ public class HostedUpdateWebhooker : IHostedService /// /// Initiallizes new instance of /// - /// /// /// /// @@ -48,17 +47,6 @@ public class HostedUpdateWebhooker : IHostedService return Task.CompletedTask; } - private async void StartInternal(CancellationToken cancellationToken) - { - await _botClient.SetWebhook( - url: _options.WebhookUri, - maxConnections: _options.MaxConnections, - allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes, - dropPendingUpdates: _options.DropPendingUpdates, - secretToken: _options.SecretToken, - cancellationToken: cancellationToken); - } - /// public Task StopAsync(CancellationToken cancellationToken) { @@ -66,16 +54,50 @@ public class HostedUpdateWebhooker : IHostedService return Task.CompletedTask; } + /// + /// Allows to remap receiving webhook endpoint and map new route to webhost. + /// + /// + /// + /// + /// + /// + public async Task RemapWebhook(IEndpointRouteBuilder routeBuilder, string webhookUri, CancellationToken cancellationToken = default) + { + if (!Uri.TryCreate(webhookUri, UriKind.Absolute, out Uri? result)) + throw new ArgumentException("invalid URL"); + + _options.WebhookUri = result.ToString(); + await SetWebhook(cancellationToken); + MapWebhook(routeBuilder); + } + /// /// Maps bot webhook to application builder /// /// - public void MapWebhook(IEndpointRouteBuilder routeBuilder) + internal void MapWebhook(IEndpointRouteBuilder routeBuilder) { string pattern = new UriBuilder(_options.WebhookUri).Path; routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate); } + private async void StartInternal(CancellationToken cancellationToken) + { + await SetWebhook(cancellationToken); + } + + private async Task SetWebhook(CancellationToken cancellationToken) + { + await _botClient.SetWebhook( + url: _options.WebhookUri, + maxConnections: _options.MaxConnections, + allowedUpdates: _updateRouter.HandlersProvider.AllowedTypes, + dropPendingUpdates: _options.DropPendingUpdates, + secretToken: _options.SecretToken, + cancellationToken: cancellationToken); + } + private async Task ReceiveUpdate(HttpContext ctx) { if (_options.SecretToken != null) diff --git a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj index e7f1904..3597694 100644 --- a/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj +++ b/src/Telegrator.Hosting.Web/Telegrator.Hosting.Web.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting.Web - 1.16.7 + 1.16.8 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator diff --git a/src/Telegrator.Hosting.Web/TypesExtensions.cs b/src/Telegrator.Hosting.Web/TypesExtensions.cs index 710af70..d4ff7e1 100644 --- a/src/Telegrator.Hosting.Web/TypesExtensions.cs +++ b/src/Telegrator.Hosting.Web/TypesExtensions.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System.Diagnostics.CodeAnalysis; using Telegram.Bot; using Telegrator.Core; using Telegrator.Hosting; @@ -45,7 +46,10 @@ namespace Telegrator /// public static ITelegramBotHostBuilder AddTelegratorWeb(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) { - builder.AddTelegratorWebInternal(options, handlers); + AddTelegratorWebInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options); + if (builder is TelegramBotWebHostBuilder telegramBotHostBuilder) + telegramBotHostBuilder._handlers = handlers; + action?.Invoke(builder); return builder; } @@ -55,7 +59,7 @@ namespace Telegrator /// public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) { - builder.AddTelegratorWebInternal(options, handlers); + AddTelegratorWebInternal(builder.Services, builder.Configuration, ((IHostApplicationBuilder)builder).Properties, ref handlers, options); action?.Invoke(new TelegramBotWebHostBuilder(builder)); return builder; } @@ -63,11 +67,8 @@ namespace Telegrator /// /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// - internal static IHostApplicationBuilder AddTelegratorWebInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + internal static void AddTelegratorWebInternal(IServiceCollection services, IConfiguration configuration, IDictionary properties, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null) { - IServiceCollection services = builder.Services; - IConfigurationManager configuration = builder.Configuration; - if (options == null) { options = configuration.GetSection(nameof(TelegratorOptions)).Get(); @@ -92,10 +93,7 @@ namespace Telegrator handlers ??= new HostHandlersCollection(services, options); services.AddSingleton(handlers); - - builder.Properties.Add(HandlersCollectionPropertyKey, handlers); - if (builder is TelegramBotWebHostBuilder botHostBuilder) - botHostBuilder._handlers = handlers; + properties.Add(HandlersCollectionPropertyKey, handlers); if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions))) { @@ -117,16 +115,27 @@ namespace Telegrator services.AddTelegramBotHostDefaults(); services.AddTelegramWebhook(); - return builder; + } + + /// + /// Searchs for hosted service inside hosts services + /// + /// + /// + /// + public static bool TryFindWebhooker(this IServiceProvider services, [NotNullWhen(true)] out HostedUpdateWebhooker? webhooker) + { + webhooker = services.GetServices().FirstOrDefault(s => s is HostedUpdateWebhooker) as HostedUpdateWebhooker; + return webhooker != null; } /// /// Replaces the initialization logic from TelegramBotWebHost constructor. /// Initializes the bot and logs handlers on application startup. /// - public static T UseTelegratorWeb(this T app) where T : IEndpointRouteBuilder, IHost + public static T UseTelegratorWeb(this T app, bool dontMap = false) where T : IEndpointRouteBuilder, IHost { - if (app.ServiceProvider.GetServices().FirstOrDefault(s => s is HostedUpdateWebhooker) is not HostedUpdateWebhooker webhooker) + if (!app.ServiceProvider.TryFindWebhooker(out HostedUpdateWebhooker? webhooker)) throw new InvalidOperationException("No service for type 'Telegrator.Mediation.HostedUpdateWebhooker' has been registered."); ITelegramBotInfo info = app.ServiceProvider.GetRequiredService(); @@ -141,7 +150,25 @@ namespace Telegrator logger.LogHandlers(handlers); } - webhooker.MapWebhook(app); + if (!dontMap) + webhooker.MapWebhook(app); + + return app; + } + + /// + /// Allows to remap receiving webhook endpoint and map new route to webhost. + /// + /// + /// + /// + /// + public static T RemapWebhook(this T app, string webhookUri) where T : IEndpointRouteBuilder, IHost + { + if (!app.ServiceProvider.TryFindWebhooker(out HostedUpdateWebhooker? webhooker)) + throw new InvalidOperationException("No service for type 'Telegrator.Mediation.HostedUpdateWebhooker' has been registered."); + + webhooker.RemapWebhook(app, webhookUri, default).GetAwaiter().GetResult(); return app; } diff --git a/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs index 1d56912..6cccc4d 100644 --- a/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs +++ b/src/Telegrator.Hosting/Hosting/ITelegramBotHostBuilder.cs @@ -1,11 +1,11 @@ using Microsoft.Extensions.Hosting; -using System; -using System.Collections.Generic; -using System.Text; using Telegrator.Core; namespace Telegrator.Hosting; +/// +/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. +/// public interface ITelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider { diff --git a/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs b/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs index 1072548..65d9563 100644 --- a/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs +++ b/src/Telegrator.Hosting/Hosting/TelegramBotHost.cs @@ -51,7 +51,7 @@ public class TelegramBotHost : IHost, ITelegratorBot public static TelegramBotHostBuilder CreateBuilder() { HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings: null); - TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, null); + TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder); return builder; } @@ -62,7 +62,7 @@ public class TelegramBotHost : IHost, ITelegratorBot public static TelegramBotHostBuilder CreateBuilder(HostApplicationBuilderSettings? settings) { HostApplicationBuilder innerBuilder = new HostApplicationBuilder(settings); - TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder, settings); + TelegramBotHostBuilder builder = new TelegramBotHostBuilder(innerBuilder); return builder; } @@ -73,7 +73,7 @@ public class TelegramBotHost : IHost, ITelegratorBot public static TelegramBotHostBuilder CreateEmptyBuilder() { HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null); - return new TelegramBotHostBuilder(innerBuilder, null); + return new TelegramBotHostBuilder(innerBuilder); } /// @@ -82,8 +82,8 @@ public class TelegramBotHost : IHost, ITelegratorBot /// public static TelegramBotHostBuilder CreateEmptyBuilder(HostApplicationBuilderSettings? settings) { - HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(null); - return new TelegramBotHostBuilder(innerBuilder, settings); + HostApplicationBuilder innerBuilder = Host.CreateEmptyApplicationBuilder(settings); + return new TelegramBotHostBuilder(innerBuilder); } /// diff --git a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs index 485e0fc..faa72a5 100644 --- a/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs +++ b/src/Telegrator.Hosting/Hosting/TelegramBotHostBuilder.cs @@ -8,13 +8,10 @@ using Telegrator.Core; #pragma warning disable IDE0001 namespace Telegrator.Hosting; -/// -/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. -/// +/// public class TelegramBotHostBuilder : ITelegramBotHostBuilder { private readonly HostApplicationBuilder _innerBuilder; - private readonly HostApplicationBuilderSettings _settings; internal IHandlersCollection _handlers = null!; /// @@ -42,23 +39,9 @@ public class TelegramBotHostBuilder : ITelegramBotHostBuilder /// Initializes a new instance of the class. /// /// - /// - public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, HostApplicationBuilderSettings? settings = null) + public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder) { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); - _settings = settings ?? new HostApplicationBuilderSettings(); - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// - public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, TelegratorOptions? options, HostApplicationBuilderSettings? settings) - { - _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); - _settings = settings ?? new HostApplicationBuilderSettings(); } /// @@ -66,24 +49,10 @@ public class TelegramBotHostBuilder : ITelegramBotHostBuilder /// /// /// - /// - public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, HostApplicationBuilderSettings? settings) + public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers) { _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); - _settings = settings ?? new HostApplicationBuilderSettings(); - } - - /// - /// Initializes a new instance of the class. - /// - /// - /// - /// - /// - public TelegramBotHostBuilder(HostApplicationBuilder hostApplicationBuilder, IHandlersCollection handlers, TelegratorOptions? options, HostApplicationBuilderSettings? settings) - { - _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); - _settings = settings ?? new HostApplicationBuilderSettings(); + _handlers = handlers ?? throw new ArgumentNullException(nameof(handlers)); } /// diff --git a/src/Telegrator.Hosting/Telegrator.Hosting.csproj b/src/Telegrator.Hosting/Telegrator.Hosting.csproj index e28ce2c..05e0b09 100644 --- a/src/Telegrator.Hosting/Telegrator.Hosting.csproj +++ b/src/Telegrator.Hosting/Telegrator.Hosting.csproj @@ -15,7 +15,7 @@ True Telegrator.Hosting - 1.16.7 + 1.16.8 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator @@ -31,8 +31,8 @@ - - + + diff --git a/src/Telegrator.Hosting/TypesExtensions.cs b/src/Telegrator.Hosting/TypesExtensions.cs index 1b10664..e9ae080 100644 --- a/src/Telegrator.Hosting/TypesExtensions.cs +++ b/src/Telegrator.Hosting/TypesExtensions.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System.Diagnostics.CodeAnalysis; using System.Text; using Telegram.Bot; using Telegram.Bot.Polling; @@ -49,7 +50,10 @@ public static class HostBuilderExtensions /// public static ITelegramBotHostBuilder AddTelegrator(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) { - builder.AddTelegratorInternal(options, handlers); + AddTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options); + if (builder is TelegramBotHostBuilder telegramBotHostBuilder) + telegramBotHostBuilder._handlers = handlers; + action?.Invoke(builder); return builder; } @@ -59,19 +63,34 @@ public static class HostBuilderExtensions /// public static IHostApplicationBuilder AddTelegrator(this HostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action? action = null) { - builder.AddTelegratorInternal(options, handlers); - action?.Invoke(new TelegramBotHostBuilder(builder)); + AddTelegratorInternal(builder.Services, builder.Configuration, ((IHostApplicationBuilder)builder).Properties, ref handlers, options); + action?.Invoke(new TelegramBotHostBuilder(builder, handlers)); return builder; } /// /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. /// - public static IHostApplicationBuilder AddTelegratorInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) { - IServiceCollection services = builder.Services; - IConfigurationManager configuration = builder.Configuration; + AddTelegratorInternal(builder.Services, builder.Configuration, builder.Properties, ref handlers, options); + return builder; + } + /// + /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + /// + public static IHostBuilder AddTelegrator(this IHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) + { + builder.ConfigureServices((ctx, sp) => AddTelegratorInternal(sp, ctx.Configuration, builder.Properties, ref handlers, options)); + return builder; + } + + /// + /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers. + /// + internal static void AddTelegratorInternal(IServiceCollection services, IConfiguration configuration, IDictionary properties, [NotNull] ref IHandlersCollection? handlers, TelegratorOptions? options = null) + { if (options == null) { options = configuration.GetSection(nameof(TelegratorOptions)).Get(); @@ -96,10 +115,7 @@ public static class HostBuilderExtensions handlers ??= new HostHandlersCollection(services, options); services.AddSingleton(handlers); - - builder.Properties.Add(HandlersCollectionPropertyKey, handlers); - if (builder is TelegramBotHostBuilder botHostBuilder) - botHostBuilder._handlers = handlers; + properties.Add(HandlersCollectionPropertyKey, handlers); if (!services.Any(srvc => srvc.ImplementationType == typeof(IOptions))) { @@ -119,9 +135,8 @@ public static class HostBuilderExtensions })); } - services.AddTelegramReceiver(); services.AddTelegramBotHostDefaults(); - return builder; + services.AddTelegramReceiver(); } } diff --git a/src/Telegrator/Telegrator.csproj b/src/Telegrator/Telegrator.csproj index d2cc076..469cc7e 100644 --- a/src/Telegrator/Telegrator.csproj +++ b/src/Telegrator/Telegrator.csproj @@ -14,7 +14,7 @@ True Telegrator : Telegram.Bot mediator framework - 1.16.7 + 1.16.8 Rikitav Tim4ik Rikitav Tim4ik https://github.com/Rikitav/Telegrator @@ -32,8 +32,8 @@ - - + +