diff --git a/Telegrator/Handlers/Components/UpdateHandlerBase.cs b/Telegrator/Handlers/Components/UpdateHandlerBase.cs
index 22fc0b1..f243695 100644
--- a/Telegrator/Handlers/Components/UpdateHandlerBase.cs
+++ b/Telegrator/Handlers/Components/UpdateHandlerBase.cs
@@ -9,7 +9,7 @@ namespace Telegrator.Handlers.Components
///
/// Base class for update handlers, providing execution and lifetime management for Telegram updates.
///
- public abstract class UpdateHandlerBase(UpdateType handlingUpdateType)
+ public abstract class UpdateHandlerBase(UpdateType handlingUpdateType) : IDisposable
{
///
/// Gets the that this handler processes.
@@ -101,6 +101,16 @@ namespace Telegrator.Handlers.Components
/// A representing the asynchronous operation.
protected abstract Task ExecuteInternal(IHandlerContainer container, CancellationToken cancellationToken);
+ ///
+ /// Dispose resources of this handler. Override if needed
+ ///
+ ///
+ /// Return if dispose was successfull and garbage collecting for this object can be supressed
+ protected virtual bool Dispose(bool disposing)
+ {
+ return false;
+ }
+
///
/// Handles failed filters during handler describing.
/// Use to control how router should treat this fail.
@@ -115,5 +125,15 @@ namespace Telegrator.Handlers.Components
{
return Task.FromResult(Result.Ok());
}
+
+ ///
+ public void Dispose()
+ {
+ if (LifetimeToken.IsEnded)
+ return;
+
+ if (Dispose(true))
+ GC.SuppressFinalize(this);
+ }
}
}
diff --git a/Telegrator/Polling/UpdateHandlersPool.cs b/Telegrator/Polling/UpdateHandlersPool.cs
index 22023e4..c9f7899 100644
--- a/Telegrator/Polling/UpdateHandlersPool.cs
+++ b/Telegrator/Polling/UpdateHandlersPool.cs
@@ -1,4 +1,5 @@
-using Telegrator.Logging;
+using Telegrator.Handlers.Components;
+using Telegrator.Logging;
using Telegrator.MadiatorCore;
using Telegrator.MadiatorCore.Descriptors;
@@ -79,20 +80,32 @@ namespace Telegrator.Polling
Alligator.LogDebug("Described handler '{0}'", handlerInfo.DisplayString);
HandlerExecuting?.Invoke(handlerInfo);
- lastResult = await handlerInfo.HandlerInstance.Execute(handlerInfo);
- ExecutingHandlersSemaphore?.Release(1);
+ using (UpdateHandlerBase instance = handlerInfo.HandlerInstance)
+ {
+ lastResult = await instance.Execute(handlerInfo);
+ ExecutingHandlersSemaphore?.Release(1);
+ }
if (lastResult.RouteNext)
{
Alligator.LogDebug("Handler requested route continuation");
}
}
+ catch (NotImplementedException)
+ {
+ _ = 0xBAD + 0xC0DE;
+ }
catch (OperationCanceledException)
{
+ _ = 0xBAD + 0xC0DE;
break;
}
+ catch (Exception ex)
+ {
+ Alligator.LogError("Failed to process handler!", ex);
+ }
- if (!lastResult.RouteNext)
+ if (lastResult != null && !lastResult.RouteNext)
break;
}
}