soxtoby / SlackNet

A comprehensive Slack API client for .NET
MIT License
207 stars 65 forks source link

await call is never completed if there is an error #207

Open Glebby opened 1 month ago

Glebby commented 1 month ago

Hello, I am trying to follow the AspNetCoreExample to post messages in Socket mode, everything works fine except when I am invoking any method, like

var response = await slackClient.Chat.PostMessage(...); 

and there is an error from Slack (for example channel_not_found), the code withing the event handler doesn't throws an exception, and the debugger doesn't go to the next line, so I cannot check the response. It seems the async call is never completed. The same behavior is with any other method where Slack returns an error (as seen in the console log).

I am using .NET 8.0 console web application, and the bootstrap code in my program.cs looks like

using SlackNet;
using SlackNet.AspNetCore;
using SlackNet.Events;
...
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args); 
            builder.Services.AddSlackNet(c => c
                .UseApiToken(userToken) // This gets used by the API client
                .UseAppLevelToken(appToken) // (Optional) used for socket mode
                .RegisterEventHandler<MessageEvent, SlackEventHandler>()
            );
            ...
            app.UseSlackNet(c => c
                .UseSocketMode(true)
            );
            app.Run();
        }
        private class SlackEventHandler(ISlackApiClient slack) : IEventHandler<MessageEvent>
        {
            public async Task Handle(MessageEvent slackEvent)
            {
                 ...
                 var response = await slackClient.Chat.PostMessage(...);
        }

Please advise. Kind Regards Gleb

soxtoby commented 1 month ago

Hi @Glebby, I tried to reproduce this with the following code:

var builder = WebApplication.CreateBuilder(args);
var slackSettings = builder.Configuration.GetSection("Slack").Get<SlackSettings>();

builder.Services.AddSlackNet(c => c
    .UseApiToken(slackSettings.ApiToken)
    .UseAppLevelToken(slackSettings.AppLevelToken)
    .UseSigningSecret(slackSettings.SigningSecret)
    .RegisterEventHandler<MessageEvent, SlackEventHandler>()
);

var app = builder.Build();
app.UseSlackNet(c => c.UseSocketMode(true));
app.Run();

record SlackSettings
{
    public string ApiToken { get; init; } = string.Empty;
    public string AppLevelToken { get; init; } = string.Empty;
    public string SigningSecret { get; init; } = string.Empty;
}

public class SlackEventHandler(ISlackApiClient slack) : IEventHandler<MessageEvent>
{
    public async Task Handle(MessageEvent slackEvent)
    {
        try
        {
            await slack.Chat.PostMessage(new Message { Channel = "C1234567890", Text = "Hello, Slack!" });
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
        }
    }
}

While debugging, it did break when the exception was thrown, and the Console.WriteLine was hit and logged the error.

You might want to check your IDE's debugging settings to make sure it's breaking on all exceptions, and that you haven't enabled Just My Code or an equivalent setting.

Hope this helps.