diff --git a/SlackNet.AspNetCore/SlackEndpointConfiguration.cs b/SlackNet.AspNetCore/SlackEndpointConfiguration.cs index 85d88c3..e8dd625 100644 --- a/SlackNet.AspNetCore/SlackEndpointConfiguration.cs +++ b/SlackNet.AspNetCore/SlackEndpointConfiguration.cs @@ -71,10 +71,22 @@ public SlackEndpointConfiguration UseSocketMode(bool useSocketMode = true, Socke return this; } + /// + /// Delay responding to Slack until after handlers have completed. + /// May be required in environments that pause or shut down after a request has been completed. + /// + [Obsolete(Warning.Experimental)] + public SlackEndpointConfiguration DelayResponse(bool delay = true) + { + DelayedResponse = delay; + return this; + } + public string RoutePrefix { get; private set; } = "slack"; public string VerificationToken { get; private set; } public string SigningSecret { get; private set; } public bool VerifyEventUrl { get; private set; } = true; public bool SocketMode { get; private set; } + public bool DelayedResponse { get; private set; } public SocketModeConnectionOptions SocketModeConnectionOptions { get; set; } } \ No newline at end of file diff --git a/SlackNet.AspNetCore/SlackRequestMiddleware.cs b/SlackNet.AspNetCore/SlackRequestMiddleware.cs index eae57ff..f5aa6bf 100644 --- a/SlackNet.AspNetCore/SlackRequestMiddleware.cs +++ b/SlackNet.AspNetCore/SlackRequestMiddleware.cs @@ -34,18 +34,31 @@ public async Task Invoke(HttpContext context) await _next(context).ConfigureAwait(false); } - private static async Task Respond(HttpResponse httpResponse, SlackResult slackResult) + private async Task Respond(HttpResponse httpResponse, SlackResult slackResult) { - httpResponse.StatusCode = (int)slackResult.Status; + try + { + if (_configuration.DelayedResponse) + { + foreach (var callback in slackResult.RequestCompletedCallbacks) + await callback().ConfigureAwait(false); + } + else + { + // Note: HttpResponse's completed callbacks are called in FILO order + foreach (var callback in slackResult.RequestCompletedCallbacks.Reverse()) + httpResponse.OnCompleted(callback); + } + } + finally + { + httpResponse.StatusCode = (int)slackResult.Status; - // Note: HttpResponse's completed callbacks are called in FILO order - foreach (var callback in slackResult.RequestCompletedCallbacks.Reverse()) - httpResponse.OnCompleted(callback); + if (slackResult.ContentType != null) + httpResponse.ContentType = slackResult.ContentType; - if (slackResult.ContentType != null) - httpResponse.ContentType = slackResult.ContentType; - - if (slackResult.Body != null) - await httpResponse.WriteAsync(slackResult.Body).ConfigureAwait(false); + if (slackResult.Body != null) + await httpResponse.WriteAsync(slackResult.Body).ConfigureAwait(false); + } } } \ No newline at end of file diff --git a/SlackNet.AspNetCore/Warning.cs b/SlackNet.AspNetCore/Warning.cs new file mode 100644 index 0000000..496aba7 --- /dev/null +++ b/SlackNet.AspNetCore/Warning.cs @@ -0,0 +1,6 @@ +namespace SlackNet.AspNetCore; + +static class Warning +{ + public const string Experimental = "This API is experimental, and likely to change in the future."; +} \ No newline at end of file