From f0a2da7e2335f7a916d698592a55afb56e8e46fa Mon Sep 17 00:00:00 2001 From: Simon Oxtoby Date: Wed, 25 Sep 2024 12:32:34 +1000 Subject: [PATCH] Socket mode client is run as an IHostedService in ASP.NET Deprecated SlackEndpointConfiguration.SocketModeConnectionOptions setter --- SlackNet.AspNetCore/AspNetCoreExtensions.cs | 14 ++----- .../AspNetSlackServiceConfiguration.cs | 1 + .../SlackEndpointConfiguration.cs | 2 +- .../SlackNet.AspNetCore.csproj | 1 + SlackNet.AspNetCore/SocketModeService.cs | 38 +++++++++++++++++++ 5 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 SlackNet.AspNetCore/SocketModeService.cs diff --git a/SlackNet.AspNetCore/AspNetCoreExtensions.cs b/SlackNet.AspNetCore/AspNetCoreExtensions.cs index 061bd7a..31e5b74 100644 --- a/SlackNet.AspNetCore/AspNetCoreExtensions.cs +++ b/SlackNet.AspNetCore/AspNetCoreExtensions.cs @@ -24,17 +24,11 @@ public static IServiceCollection AddSlackNet(this IServiceCollection serviceColl /// public static IApplicationBuilder UseSlackNet(this IApplicationBuilder app, Action configure = null) { - var config = new SlackEndpointConfiguration(); + var config = app.ApplicationServices.GetRequiredService(); configure?.Invoke(config); - if (config.SocketMode) - { - app.ApplicationServices.GetRequiredService().Connect(); - return app; - } - else - { - return app.UseMiddleware(config); - } + return config.SocketMode + ? app // Nothing to do - SocketModeService manages the client + : app.UseMiddleware(config); } } \ No newline at end of file diff --git a/SlackNet.AspNetCore/AspNetSlackServiceConfiguration.cs b/SlackNet.AspNetCore/AspNetSlackServiceConfiguration.cs index ce540f4..e84b574 100644 --- a/SlackNet.AspNetCore/AspNetSlackServiceConfiguration.cs +++ b/SlackNet.AspNetCore/AspNetSlackServiceConfiguration.cs @@ -34,6 +34,7 @@ protected internal AspNetSlackServiceConfiguration(IServiceCollection serviceCol ServiceCollection.TryAddSingleton(); ServiceCollection.TryAddSingleton(); ServiceCollection.TryAddSingleton(); + ServiceCollection.AddHostedService(); base.ConfigureServices(); } diff --git a/SlackNet.AspNetCore/SlackEndpointConfiguration.cs b/SlackNet.AspNetCore/SlackEndpointConfiguration.cs index e8dd625..c7bd846 100644 --- a/SlackNet.AspNetCore/SlackEndpointConfiguration.cs +++ b/SlackNet.AspNetCore/SlackEndpointConfiguration.cs @@ -88,5 +88,5 @@ public SlackEndpointConfiguration DelayResponse(bool delay = true) public bool VerifyEventUrl { get; private set; } = true; public bool SocketMode { get; private set; } public bool DelayedResponse { get; private set; } - public SocketModeConnectionOptions SocketModeConnectionOptions { get; set; } + public SocketModeConnectionOptions SocketModeConnectionOptions { get; [Obsolete("Pass options to UseSocketMode instead.")] set; } } \ No newline at end of file diff --git a/SlackNet.AspNetCore/SlackNet.AspNetCore.csproj b/SlackNet.AspNetCore/SlackNet.AspNetCore.csproj index 7d5231c..b86f052 100644 --- a/SlackNet.AspNetCore/SlackNet.AspNetCore.csproj +++ b/SlackNet.AspNetCore/SlackNet.AspNetCore.csproj @@ -29,6 +29,7 @@ + diff --git a/SlackNet.AspNetCore/SocketModeService.cs b/SlackNet.AspNetCore/SocketModeService.cs new file mode 100644 index 0000000..bf9722d --- /dev/null +++ b/SlackNet.AspNetCore/SocketModeService.cs @@ -0,0 +1,38 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace SlackNet.AspNetCore; + +class SocketModeService( + ISlackSocketModeClient socketModeClient, + ILogger logger, + SlackEndpointConfiguration config +) : IHostedService +{ + private readonly bool _enabled = config.SocketMode; // Holding onto this to ensure initial state is used when stopping + + public async Task StartAsync(CancellationToken cancellationToken) + { + if (_enabled) + { + try + { + await socketModeClient.Connect(config.SocketModeConnectionOptions, cancellationToken).ConfigureAwait(false); + } + catch (Exception e) + { + logger.LogError(e, "Error connecting Slack socket mode client"); + } + } + } + + public Task StopAsync(CancellationToken cancellationToken) + { + if (_enabled) + socketModeClient.Disconnect(); + return Task.CompletedTask; + } +} \ No newline at end of file