From 8e0d2719014bea6c2632d670a30e531efafe354d Mon Sep 17 00:00:00 2001 From: Rikitav Date: Sat, 2 Aug 2025 03:19:52 +0400 Subject: [PATCH] * Fixed method handlers behaviour --- .../TelegramBotWebHostBuilder.cs | 1 + Telegrator.Hosting/TelegramBotHostBuilder.cs | 2 +- Telegrator.sln | 8 ++++++++ .../Descriptors/DefaultCustomDescriptors.cs | 18 ++++++++++++++---- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Telegrator.Hosting.Web/TelegramBotWebHostBuilder.cs b/Telegrator.Hosting.Web/TelegramBotWebHostBuilder.cs index 65b9402..bec7707 100644 --- a/Telegrator.Hosting.Web/TelegramBotWebHostBuilder.cs +++ b/Telegrator.Hosting.Web/TelegramBotWebHostBuilder.cs @@ -70,6 +70,7 @@ namespace Telegrator.Hosting.Web Services.Configure(Configuration.GetSection(nameof(TelegramBotClientOptions)), new TelegramBotClientOptionsProxy()); } + Services.AddSingleton(Configuration); Services.AddSingleton>(Options.Create(_settings)); return new TelegramBotWebHost(_innerBuilder, _handlers); } diff --git a/Telegrator.Hosting/TelegramBotHostBuilder.cs b/Telegrator.Hosting/TelegramBotHostBuilder.cs index 0bba6bc..69a0606 100644 --- a/Telegrator.Hosting/TelegramBotHostBuilder.cs +++ b/Telegrator.Hosting/TelegramBotHostBuilder.cs @@ -47,7 +47,6 @@ namespace Telegrator.Hosting { _innerBuilder = hostApplicationBuilder; _settings = settings ?? new TelegramBotHostBuilderSettings(); - _handlers = new HostHandlersCollection(Services, _settings); _innerBuilder.Logging.ClearProviders(); @@ -78,6 +77,7 @@ namespace Telegrator.Hosting } Services.AddSingleton>(Options.Create(_settings)); + Services.AddSingleton(Configuration); return new TelegramBotHost(_innerBuilder, _handlers); } } diff --git a/Telegrator.sln b/Telegrator.sln index f9bed94..88c8e42 100644 --- a/Telegrator.sln +++ b/Telegrator.sln @@ -20,6 +20,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Analyzers", "Tel EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Telegrator.Hosting.Web", "Telegrator.Hosting.Web\Telegrator.Hosting.Web.csproj", "{98AB490F-6A36-CCFF-F6E6-B029D1665965}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SosalBot", "..\SosalBot\SosalBot\SosalBot.csproj", "{D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution AnalyzersDebug|Any CPU = AnalyzersDebug|Any CPU @@ -63,6 +65,12 @@ Global {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Debug|Any CPU.Build.0 = Debug|Any CPU {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.ActiveCfg = Release|Any CPU {98AB490F-6A36-CCFF-F6E6-B029D1665965}.Release|Any CPU.Build.0 = Release|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.ActiveCfg = Release|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.AnalyzersDebug|Any CPU.Build.0 = Release|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6AA4D47-0DCE-520E-5779-A14EA9CB1DEC}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs b/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs index f8a7eaf..b34f4b4 100644 --- a/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs +++ b/Telegrator/MadiatorCore/Descriptors/DefaultCustomDescriptors.cs @@ -15,6 +15,8 @@ namespace Telegrator.MadiatorCore.Descriptors /// public class MethodHandlerDescriptor : HandlerDescriptor where TUpdate : class { + private readonly MethodInfo Method; + /// /// Initializes new instance of /// @@ -28,13 +30,21 @@ namespace Telegrator.MadiatorCore.Descriptors UpdateType = handlerAttribute.Type; Indexer = handlerAttribute.GetIndexer(); Filters = new DescriptorFiltersSet(handlerAttribute, stateKeeperAttribute, filters); - DisplayString = HandlerInspector.GetDisplayName(action.Method); - InstanceFactory = () => new MethodHandler(action.Method, UpdateType); + DisplayString = HandlerInspector.GetDisplayName(action.Method) ?? action.Method.Name; + Method = action.Method; + InstanceFactory = () => new MethodHandler(UpdateType); + LazyInitialization = handler => + { + if (handler is not MethodHandler methodHandler) + throw new InvalidDataException(); + + methodHandler.Method = Method; + }; } - private class MethodHandler(MethodInfo method, UpdateType updateType) : AbstractUpdateHandler(updateType) + private class MethodHandler(UpdateType updateType) : AbstractUpdateHandler(updateType) { - private readonly MethodInfo Method = method; + internal MethodInfo Method = null!; public override async Task Execute(IAbstractHandlerContainer container, CancellationToken cancellation) {