Isolated process HttpTriggers Custom Middleware response.WriteAsJsonAsync creates an exception
- Dominant language
- PowerShell
- Stars
- 1.1k
- Forks
- 215
- Avg merge
- 4h 2m
- Merged PRs (30d)
- 1
Description
in c# net8 V4 InProcess LTS azure functions
If in middleware i create a response which usings WriteAsJsonAsync is causes the following Exception
``
An attempt was made to transition a task to a final state when it had already completed.
``
if I change it to WriteStringAsync i don't get the exception, it would be nice to have the same availability to return json.
This only happens in custom middlware but no matter where i put it i still get the same exception, this only happens after the return of the middleware takes place but it is the WriteStringAsync that causes the exception
``
public async Task Invoke(FunctionContext context, FunctionExecutionDelegate next)
{
var requestData = await context.GetHttpRequestDataAsync() ?? throw new InvalidOperationException(nameof(HttpRequestData));
// await new CorsResponse(context, requestData, _log).ErrorAsync("testing", "something ");
// return;
var foo = "foo";
var bar = "bar";
_log.LogInformation("setting error response");
var responseData =requestData.CreateResponse();
responseData.Headers.Add("foo", "bar");
_log.LogInformation("error {0} {1}", "error", "errorDescription");
await responseData.WriteAsJsonAsync(new
{
foo,
bar
}, HttpStatusCode.Unauthorized
);
_log.LogInformation("set error response");
context.GetInvocationResult().Value = responseData;
_log.LogInformation("added error response to invocation result");
return;
``
on a side note WriteAsJsonAsync also always resets the stattus code to OK another annoying function of WriteAsJsonAsync if i set it in create it should stay what i set it.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.