* Added building delegate to UseTelegrator method

* Fixed Webhooker trying to resolve IEndpointRouteBuildr from DI
This commit is contained in:
2026-03-15 17:20:53 +04:00
parent 0dedd3c0f4
commit 3d2d21f6c0
13 changed files with 127 additions and 58 deletions
+22 -3
View File
@@ -28,6 +28,9 @@
This application's logger This application's logger
</summary> </summary>
</member> </member>
<member name="P:Telegrator.Hosting.Web.TelegramBotWebHost.Properties">
<inheritdoc/>
</member>
<member name="M:Telegrator.Hosting.Web.TelegramBotWebHost.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder)"> <member name="M:Telegrator.Hosting.Web.TelegramBotWebHost.#ctor(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<summary> <summary>
Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/> class. Initializes a new instance of the <see cref="T:Microsoft.AspNetCore.Builder.WebApplicationBuilder"/> class.
@@ -181,7 +184,7 @@
Service for receiving updates for Hosted telegram bots via Webhooks Service for receiving updates for Hosted telegram bots via Webhooks
</summary> </summary>
</member> </member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.#ctor(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder,Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.Hosting.Web.WebhookerOptions})"> <member name="M:Telegrator.Mediation.HostedUpdateWebhooker.#ctor(Telegram.Bot.ITelegramBotClient,Telegrator.Core.IUpdateRouter,Microsoft.Extensions.Options.IOptions{Telegrator.Hosting.Web.WebhookerOptions})">
<summary> <summary>
Initiallizes new instance of <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/> Initiallizes new instance of <see cref="T:Telegrator.Mediation.HostedUpdateWebhooker"/>
</summary> </summary>
@@ -197,6 +200,12 @@
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.StopAsync(System.Threading.CancellationToken)"> <member name="M:Telegrator.Mediation.HostedUpdateWebhooker.StopAsync(System.Threading.CancellationToken)">
<inheritdoc/> <inheritdoc/>
</member> </member>
<member name="M:Telegrator.Mediation.HostedUpdateWebhooker.MapWebhook(Microsoft.AspNetCore.Routing.IEndpointRouteBuilder)">
<summary>
Maps bot webhook to application builder
</summary>
<param name="routeBuilder"></param>
</member>
<member name="T:Telegrator.ServicesCollectionExtensions"> <member name="T:Telegrator.ServicesCollectionExtensions">
<summary> <summary>
Contains extensions for <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection"/> Contains extensions for <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection"/>
@@ -211,12 +220,22 @@
<member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)"> <member name="M:Telegrator.ServicesCollectionExtensions.get_Handlers(Microsoft.AspNetCore.Builder.WebApplicationBuilder)">
<inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.&lt;G&gt;$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/> <inheritdoc cref="P:Telegrator.ServicesCollectionExtensions.&lt;G&gt;$41F16C2D39AF52899E745C9C9F42FF83.Handlers"/>
</member> </member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)"> <member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Telegrator.Hosting.ITelegramBotHostBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary> <summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.ServicesCollectionExtensions.UseTelegratorWeb(Microsoft.AspNetCore.Builder.WebApplication)"> <member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWeb(Microsoft.AspNetCore.Builder.WebApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.AddTelegratorWebInternal(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.ServicesCollectionExtensions.UseTelegratorWeb``1(``0)">
<summary> <summary>
Replaces the initialization logic from TelegramBotWebHost constructor. Replaces the initialization logic from TelegramBotWebHost constructor.
Initializes the bot and logs handlers on application startup. Initializes the bot and logs handlers on application startup.
+15 -5
View File
@@ -255,15 +255,25 @@
The key used to store the <see cref="T:Telegrator.Core.IHandlersCollection"/> in the builder properties. The key used to store the <see cref="T:Telegrator.Core.IHandlersCollection"/> in the builder properties.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.IHostApplicationBuilder)"> <member name="M:Telegrator.HostBuilderExtensions.get_Handlers(Microsoft.Extensions.Hosting.HostApplicationBuilder)">
<inheritdoc cref="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$605D8CCF64349EA050C790D67C500BD9.Handlers"/> <inheritdoc cref="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$BF7227490CCA365283B3A9274C9033C7.Handlers"/>
</member> </member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)"> <member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Telegrator.Hosting.ITelegramBotHostBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary> <summary>
Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary> </summary>
</member> </member>
<member name="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$605D8CCF64349EA050C790D67C500BD9.Handlers"> <member name="M:Telegrator.HostBuilderExtensions.AddTelegrator(Microsoft.Extensions.Hosting.HostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection,System.Action{Telegrator.Hosting.ITelegramBotHostBuilder})">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="M:Telegrator.HostBuilderExtensions.AddTelegratorInternal(Microsoft.Extensions.Hosting.IHostApplicationBuilder,Telegrator.TelegratorOptions,Telegrator.Core.IHandlersCollection)">
<summary>
Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
</summary>
</member>
<member name="P:Telegrator.HostBuilderExtensions.&lt;G&gt;$BF7227490CCA365283B3A9274C9033C7.Handlers">
<summary> <summary>
Gets the <see cref="T:Telegrator.Core.IHandlersCollection"/> from the builder properties. Gets the <see cref="T:Telegrator.Core.IHandlersCollection"/> from the builder properties.
</summary> </summary>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.RedisStateStorage</Title> <Title>Telegrator.RedisStateStorage</Title>
<Version>1.16.5</Version> <Version>1.16.6</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -39,11 +39,13 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
/// </summary> /// </summary>
public ILogger<TelegramBotWebHost> Logger => _logger; public ILogger<TelegramBotWebHost> Logger => _logger;
/// <inheritdoc/>
public IDictionary<string, object?> Properties => ((IApplicationBuilder)_innerApp).Properties;
// Private interface fields // Private interface fields
IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services; IServiceProvider IEndpointRouteBuilder.ServiceProvider => Services;
IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); } IServiceProvider IApplicationBuilder.ApplicationServices { get => Services; set => throw new NotImplementedException(); }
IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures; IFeatureCollection IApplicationBuilder.ServerFeatures => ((IApplicationBuilder)_innerApp).ServerFeatures;
IDictionary<string, object?> IApplicationBuilder.Properties => ((IApplicationBuilder)_innerApp).Properties;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class. /// Initializes a new instance of the <see cref="WebApplicationBuilder"/> class.
@@ -68,9 +70,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateBuilder(settings);
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
builder.AddTelegratorWeb();
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramWebhook();
return builder; return builder;
} }
@@ -83,9 +83,7 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateSlimBuilder(settings);
TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
builder.AddTelegratorWeb();
builder.Services.AddTelegramBotHostDefaults();
builder.Services.AddTelegramWebhook();
return builder; return builder;
} }
@@ -97,7 +95,9 @@ public class TelegramBotWebHost : IHost, IApplicationBuilder, IEndpointRouteBuil
{ {
ArgumentNullException.ThrowIfNull(settings, nameof(settings)); ArgumentNullException.ThrowIfNull(settings, nameof(settings));
WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings); WebApplicationBuilder innerApp = WebApplication.CreateEmptyBuilder(settings);
return new TelegramBotWebHostBuilder(innerApp, settings); TelegramBotWebHostBuilder builder = new TelegramBotWebHostBuilder(innerApp, settings);
builder.AddTelegratorWeb();
return builder;
} }
/// <inheritdoc/> /// <inheritdoc/>
@@ -12,7 +12,7 @@ namespace Telegrator.Hosting.Web;
/// <summary> /// <summary>
/// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// Represents a web hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary> /// </summary>
public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingProvider public class TelegramBotWebHostBuilder : ITelegramBotHostBuilder
{ {
private readonly WebApplicationBuilder _innerBuilder; private readonly WebApplicationBuilder _innerBuilder;
private readonly WebApplicationOptions _settings; private readonly WebApplicationOptions _settings;
@@ -48,8 +48,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); _settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb();
} }
/// <summary> /// <summary>
@@ -62,8 +60,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); _settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb(options, null);
} }
/// <summary> /// <summary>
@@ -76,8 +72,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); _settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb(null, handlers);
} }
/// <summary> /// <summary>
@@ -91,8 +85,6 @@ public class TelegramBotWebHostBuilder : IHostApplicationBuilder, ICollectingPro
{ {
_innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder)); _innerBuilder = webApplicationBuilder ?? throw new ArgumentNullException(nameof(webApplicationBuilder));
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); _settings = settings ?? throw new ArgumentNullException(nameof(settings));
this.AddTelegratorWeb(options, handlers);
} }
/// <summary> /// <summary>
@@ -19,7 +19,6 @@ public class HostedUpdateWebhooker : IHostedService
{ {
private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token"; private const string SecretTokenHeader = "X-Telegram-Bot-Api-Secret-Token";
private readonly IEndpointRouteBuilder _botHost;
private readonly ITelegramBotClient _botClient; private readonly ITelegramBotClient _botClient;
private readonly IUpdateRouter _updateRouter; private readonly IUpdateRouter _updateRouter;
private readonly WebhookerOptions _options; private readonly WebhookerOptions _options;
@@ -32,12 +31,11 @@ public class HostedUpdateWebhooker : IHostedService
/// <param name="updateRouter"></param> /// <param name="updateRouter"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <exception cref="ArgumentNullException"></exception> /// <exception cref="ArgumentNullException"></exception>
public HostedUpdateWebhooker(IEndpointRouteBuilder botHost, ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options) public HostedUpdateWebhooker(ITelegramBotClient botClient, IUpdateRouter updateRouter, IOptions<WebhookerOptions> options)
{ {
if (string.IsNullOrEmpty(options.Value.WebhookUri)) if (string.IsNullOrEmpty(options.Value.WebhookUri))
throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving"); throw new ArgumentNullException(nameof(options), "Option \"WebhookUrl\" must be set to subscribe for update recieving");
_botHost = botHost;
_botClient = botClient; _botClient = botClient;
_updateRouter = updateRouter; _updateRouter = updateRouter;
_options = options.Value; _options = options.Value;
@@ -52,9 +50,6 @@ public class HostedUpdateWebhooker : IHostedService
private async void StartInternal(CancellationToken cancellationToken) private async void StartInternal(CancellationToken cancellationToken)
{ {
string pattern = new UriBuilder(_options.WebhookUri).Path;
_botHost.MapPost(pattern, (Delegate)ReceiveUpdate);
await _botClient.SetWebhook( await _botClient.SetWebhook(
url: _options.WebhookUri, url: _options.WebhookUri,
maxConnections: _options.MaxConnections, maxConnections: _options.MaxConnections,
@@ -71,6 +66,16 @@ public class HostedUpdateWebhooker : IHostedService
return Task.CompletedTask; return Task.CompletedTask;
} }
/// <summary>
/// Maps bot webhook to application builder
/// </summary>
/// <param name="routeBuilder"></param>
public void MapWebhook(IEndpointRouteBuilder routeBuilder)
{
string pattern = new UriBuilder(_options.WebhookUri).Path;
routeBuilder.MapPost(pattern, (Delegate)ReceiveUpdate);
}
private async Task<IResult> ReceiveUpdate(HttpContext ctx) private async Task<IResult> ReceiveUpdate(HttpContext ctx)
{ {
if (_options.SecretToken != null) if (_options.SecretToken != null)
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title> <Title>Telegrator.Hosting.Web</Title>
<Version>1.16.5</Version> <Version>1.16.6</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
+28 -5
View File
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
@@ -42,7 +43,27 @@ namespace Telegrator
/// <summary> /// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary> /// </summary>
public static IHostApplicationBuilder AddTelegratorWeb(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) public static ITelegramBotHostBuilder AddTelegratorWeb(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
builder.AddTelegratorWebInternal(options, handlers);
action?.Invoke(builder);
return builder;
}
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegratorWeb(this WebApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
builder.AddTelegratorWebInternal(options, handlers);
action?.Invoke(new TelegramBotWebHostBuilder(builder));
return builder;
}
/// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers.
/// </summary>
internal static IHostApplicationBuilder AddTelegratorWebInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{ {
IServiceCollection services = builder.Services; IServiceCollection services = builder.Services;
IConfigurationManager configuration = builder.Configuration; IConfigurationManager configuration = builder.Configuration;
@@ -103,11 +124,12 @@ namespace Telegrator
/// Replaces the initialization logic from TelegramBotWebHost constructor. /// Replaces the initialization logic from TelegramBotWebHost constructor.
/// Initializes the bot and logs handlers on application startup. /// Initializes the bot and logs handlers on application startup.
/// </summary> /// </summary>
public static WebApplication UseTelegratorWeb(this WebApplication app) public static T UseTelegratorWeb<T>(this T app) where T : IEndpointRouteBuilder, IHost
{ {
ITelegramBotInfo info = app.Services.GetRequiredService<ITelegramBotInfo>(); HostedUpdateWebhooker webhooker = app.ServiceProvider.GetRequiredService<HostedUpdateWebhooker>();
IHandlersCollection handlers = app.Services.GetRequiredService<IHandlersCollection>(); ITelegramBotInfo info = app.ServiceProvider.GetRequiredService<ITelegramBotInfo>();
ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>(); IHandlersCollection handlers = app.ServiceProvider.GetRequiredService<IHandlersCollection>();
ILoggerFactory loggerFactory = app.ServiceProvider.GetRequiredService<ILoggerFactory>();
ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost"); ILogger logger = loggerFactory.CreateLogger("Telegrator.Hosting.Web.TelegratorHost");
if (logger.IsEnabled(LogLevel.Information)) if (logger.IsEnabled(LogLevel.Information))
@@ -117,6 +139,7 @@ namespace Telegrator
logger.LogHandlers(handlers); logger.LogHandlers(handlers);
} }
webhooker.MapWebhook(app);
return app; return app;
} }
@@ -0,0 +1,12 @@
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Text;
using Telegrator.Core;
namespace Telegrator.Hosting;
public interface ITelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider
{
}
@@ -11,7 +11,7 @@ namespace Telegrator.Hosting;
/// <summary> /// <summary>
/// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more. /// Represents a hosted telegram bots and services builder that helps manage configuration, logging, lifetime, and more.
/// </summary> /// </summary>
public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvider public class TelegramBotHostBuilder : ITelegramBotHostBuilder
{ {
private readonly HostApplicationBuilder _innerBuilder; private readonly HostApplicationBuilder _innerBuilder;
private readonly HostApplicationBuilderSettings _settings; private readonly HostApplicationBuilderSettings _settings;
@@ -47,8 +47,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings(); _settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator();
} }
/// <summary> /// <summary>
@@ -61,8 +59,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings(); _settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator(options, null);
} }
/// <summary> /// <summary>
@@ -75,8 +71,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings(); _settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator(null, handlers);
} }
/// <summary> /// <summary>
@@ -90,8 +84,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
{ {
_innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder)); _innerBuilder = hostApplicationBuilder ?? throw new ArgumentNullException(nameof(hostApplicationBuilder));
_settings = settings ?? new HostApplicationBuilderSettings(); _settings = settings ?? new HostApplicationBuilderSettings();
this.AddTelegrator(options, handlers);
} }
/// <summary> /// <summary>
@@ -108,6 +100,6 @@ public class TelegramBotHostBuilder : IHostApplicationBuilder, ICollectingProvid
/// <inheritdoc/> /// <inheritdoc/>
public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull public void ConfigureContainer<TContainerBuilder>(IServiceProviderFactory<TContainerBuilder> factory, Action<TContainerBuilder>? configure = null) where TContainerBuilder : notnull
{ {
this.ConfigureContainer(factory, configure); _innerBuilder.ConfigureContainer(factory, configure);
} }
} }
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title> <Title>Telegrator.Hosting</Title>
<Version>1.16.5</Version> <Version>1.16.6</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
+24 -8
View File
@@ -30,7 +30,7 @@ public static class HostBuilderExtensions
/// </summary> /// </summary>
public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection); public const string HandlersCollectionPropertyKey = nameof(IHandlersCollection);
extension (IHostApplicationBuilder builder) extension (HostApplicationBuilder builder)
{ {
/// <summary> /// <summary>
/// Gets the <see cref="IHandlersCollection"/> from the builder properties. /// Gets the <see cref="IHandlersCollection"/> from the builder properties.
@@ -39,18 +39,35 @@ public static class HostBuilderExtensions
{ {
get get
{ {
if (builder is TelegramBotHostBuilder botHostBuilder) return (IHandlersCollection)((IHostApplicationBuilder)builder).Properties[HandlersCollectionPropertyKey];
return botHostBuilder.Handlers;
return (IHandlersCollection)builder.Properties[HandlersCollectionPropertyKey];
} }
} }
} }
/// <summary> /// <summary>
/// Replaces TelegramBotWebHostBuilder. Configures DI, options, and handlers. /// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary> /// </summary>
public static IHostApplicationBuilder AddTelegrator(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null) public static ITelegramBotHostBuilder AddTelegrator(this ITelegramBotHostBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
builder.AddTelegratorInternal(options, handlers);
action?.Invoke(builder);
return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegrator(this HostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null, Action<ITelegramBotHostBuilder>? action = null)
{
builder.AddTelegratorInternal(options, handlers);
action?.Invoke(new TelegramBotHostBuilder(builder));
return builder;
}
/// <summary>
/// Replaces TelegramBotHostBuilder. Configures DI, options, and handlers.
/// </summary>
public static IHostApplicationBuilder AddTelegratorInternal(this IHostApplicationBuilder builder, TelegratorOptions? options = null, IHandlersCollection? handlers = null)
{ {
IServiceCollection services = builder.Services; IServiceCollection services = builder.Services;
IConfigurationManager configuration = builder.Configuration; IConfigurationManager configuration = builder.Configuration;
@@ -133,7 +150,6 @@ public static class ServicesCollectionExtensions
/// <returns></returns> /// <returns></returns>
public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services) public static IServiceCollection AddTelegramBotHostDefaults(this IServiceCollection services)
{ {
services.AddLogging(builder => builder.AddConsole().AddDebug());
services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>(); services.AddSingleton<IAwaitingProvider, HostAwaitingProvider>();
services.AddSingleton<IHandlersProvider, HostHandlersProvider>(); services.AddSingleton<IHandlersProvider, HostHandlersProvider>();
services.AddSingleton<IUpdateRouter, HostUpdateRouter>(); services.AddSingleton<IUpdateRouter, HostUpdateRouter>();
+1 -1
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title> <Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.5</Version> <Version>1.16.6</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>