Skip to content

Commit

Permalink
Socket mode client is run as an IHostedService in ASP.NET
Browse files Browse the repository at this point in the history
Deprecated SlackEndpointConfiguration.SocketModeConnectionOptions setter
  • Loading branch information
soxtoby committed Sep 25, 2024
1 parent 76f3a5a commit f0a2da7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
14 changes: 4 additions & 10 deletions SlackNet.AspNetCore/AspNetCoreExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ public static IServiceCollection AddSlackNet(this IServiceCollection serviceColl
/// </summary>
public static IApplicationBuilder UseSlackNet(this IApplicationBuilder app, Action<SlackEndpointConfiguration> configure = null)
{
var config = new SlackEndpointConfiguration();
var config = app.ApplicationServices.GetRequiredService<SlackEndpointConfiguration>();
configure?.Invoke(config);

if (config.SocketMode)
{
app.ApplicationServices.GetRequiredService<ISlackSocketModeClient>().Connect();
return app;
}
else
{
return app.UseMiddleware<SlackRequestMiddleware>(config);
}
return config.SocketMode
? app // Nothing to do - SocketModeService manages the client
: app.UseMiddleware<SlackRequestMiddleware>(config);
}
}
1 change: 1 addition & 0 deletions SlackNet.AspNetCore/AspNetSlackServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected internal AspNetSlackServiceConfiguration(IServiceCollection serviceCol
ServiceCollection.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
ServiceCollection.TryAddSingleton<IRequestServiceProviderAccessor, HttpContextServiceProviderAccessor>();
ServiceCollection.TryAddSingleton<IServiceProviderSlackRequestListener, AspNetCoreServiceProviderSlackRequestListener>();
ServiceCollection.AddHostedService<SocketModeService>();

base.ConfigureServices();
}
Expand Down
2 changes: 1 addition & 1 deletion SlackNet.AspNetCore/SlackEndpointConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
1 change: 1 addition & 0 deletions SlackNet.AspNetCore/SlackNet.AspNetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="1.1.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageReference Include="System.Text.Encodings.Web" Version="4.5.1" />
</ItemGroup>
Expand Down
38 changes: 38 additions & 0 deletions SlackNet.AspNetCore/SocketModeService.cs
Original file line number Diff line number Diff line change
@@ -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<SocketModeService> 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;
}
}

0 comments on commit f0a2da7

Please sign in to comment.