Add Http Response Behavior to answer synchronously using the HttpResponse Activity (#5446)

* Update WriteHttpResponse.cs to call response.CompleteAsync()

* Add options in HttpActivityOptions

---------

Co-authored-by: Ciaran ODonnell <wannabedeveloper@live.com>
Co-authored-by: Jérémie DEVILLARD <jdevillard@users.noreply.github.com>
This commit is contained in:
jdevillard 2024-05-27 21:14:25 +02:00 committed by GitHub
parent 17c20c53f7
commit ad236f626f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 3 deletions

View file

@ -10,6 +10,8 @@ using Elsa.Workflows.UIHints;
using Elsa.Workflows.Exceptions;
using Elsa.Workflows.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;
using Elsa.Http.Options;
namespace Elsa.Http;
@ -133,7 +135,12 @@ public class WriteHttpResponse : Activity
await response.WriteAsync("The response includes a type that cannot be serialized.");
}
}
}
}
//Check if the configuration is set to flush immediatly the response to the caller.
var options = context.GetRequiredService<IOptions<HttpActivityOptions>>();
if (options.Value.WriteHttpResponseSynchronously)
await response.CompleteAsync();
// Complete activity.
await context.CompleteActivityAsync();
@ -144,4 +151,4 @@ public class WriteHttpResponse : Activity
: content is string
? "text/plain"
: "application/json";
}
}

View file

@ -35,5 +35,11 @@ public class HttpActivityOptions
"text/html",
"text/json",
"text/xml",
};
};
/// <summary>
/// A boolean to opt-in if you want to call CompleteAsync() in the HttpResponse Activity
/// and not waiting for the end of the HttpMiddleware.
/// </summary>
public Boolean WriteHttpResponseSynchronously { get; set; } = false;
}