aws / aws/aws-lambda-dotnet

Duplicate response body when running on Lambda and manipulating response body stream in middleware

Aperta
#1,185 3 commenti 2 reazioni 0 assegnatari Vedi su GitHub
bug module/aspnetcore-support needs-investigation p2 queued
Lingua principale
C#
Stelle
1.7k
Fork
503
Merge medio
1g 13h
PR unite (30g)
19

Descrizione

### Describe the bug

I have a ASP.NET middleware which temporarily replaces the HTTP response body stream with a memory stream so that it can perform work involving seek operations on the response stream before the response is sent to the client. The code works properly when running locally using Kestrel or the integration tests server, but when running on AWS, all response bodies are duplicated (i.e. `{"foo":"bar"}` becomes `{"foo":"bar"}{"foo":"bar"}`).

### Expected Behavior

The response should be a valid JSON result on all hosting environments (e.g. `{"foo":"bar"}`).

### Current Behavior

The response is invalid when running on AWS Lambda only (duplicated JSON, e.g. `{"foo":"bar"}{"foo":"bar"}`).

### Reproduction Steps

Simplified middleware code looks like this:

```csharp
public async Task InvokeAsync(HttpContext context)
{
Stream originalResponseBodyStream = context.Response.Body;
using var buffer = new MemoryStream();
context.Response.Body = buffer;

try
{
await this._next(context);
// Do work involving seek on body stream
}
finally
{
context.Response.Body.Seek(0, SeekOrigin.Begin);
await context.Response.Body.CopyToAsync(originalResponseBodyStream);
context.Response.Body = originalResponseBodyStream;
}
}
```

### Possible Solution

_No response_

### Additional Information/Context

My ASP.NET application is using the Minimal API hosting pattern. The AWS Lambda hosting mode is set to `LambdaEventSource.RestApi`.

### AWS .NET SDK and/or Package version used

* Amazon.Lambda.AspNetCoreServer 7.1.0
* Amazon.Lambda.AspNetCoreServer.Hosting 1.1.0

### Targeted .NET Platform

.NET 6

### Operating System and version

AmazonLinux

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Iniziare riproducendo il problema con il middleware fornito in Amazon.Lambda.AspNetCoreServer.Hosting, usando Minimal API hosting e LambdaEventSource.RestApi su .NET 6. Confrontare la gestione del corpo della risposta con Kestrel o con il server di test di integrazione; il lavoro è completato quando la risposta AWS Lambda contiene il corpo JSON esattamente una volta. Nel report non viene indicato alcun file del repository né alcun test.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
aws, csharp
Ambito
backend, cloud
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.