soxtoby / SlackNet

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

Can't send Http request in IEventHandler #187

Closed TimSon777 closed 6 months ago

TimSon777 commented 8 months ago

Hi! We have very strange problem: we can't send http request in Api, where we locate this IEventHandler from IEventHandler (monolith with slack api and business-logic api), because we always get error:

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException Unexpected end of request content.

image

I research it, and we can send these http requests in IBlockActionHandler and custom controllers too:

image

Main difference between these actions: the former respond instantly and process this event in the background, while the latter process it in the moment

I also research it in github and find: https://github.com/dotnet/aspnetcore/issues/23949

image

You have something similar in your code, but everything seems ok. I'm a little confused by OnCompleted in HttpResponse, of course, but it seems to be normal there too. Any ideas?

soxtoby commented 8 months ago

Hmm... this is strange. I agree the early response with events is potentially suspect, but I haven't had any luck reproducing the issue. By the time the event handler is called, nothing should be accessing the request as far as I can tell.

Is it definitely throwing during the PostAsJsonAsync call? Could it be something to do with the IHttpClientFactory configuration? I just used a plain .AddHttpClient() call to set it up.

TimSon777 commented 8 months ago

Sorry, I forgot to provide context. This works locally, but specifically in aws apprunner it does not work. I just thought maybe you know some subtle places in your code that could potentially break something

I used services.AddHttpClient(); too

soxtoby commented 8 months ago

specifically in aws apprunner it does not work

This could be similar to what I hit with Azure Functions then - the app is only meant to run for the length of the request, and handlers running after the request completes were failing. I'm not familiar with App Runner, but if it operates similarly, then the solution may be the same - delay the response until the handler has completed.

Unfortunately I only put that functionality into the Functions integration - I'll try to get a new version up in the next few days that provides the same option for ASP.NET.

soxtoby commented 8 months ago

v0.12.1 has the new option. Enable it with:

app.UseSlackNet(c => c.DelayResponse())

I've marked it as obsolete with a note about it being experimental, because the early response behaviour itself is experimental, and once I finally getting around to finishing that off, there shouldn't be a need for this option any more.

Give that a try and let me know if it works.

stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.