aws / aws/aws-lambda-dotnet

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

Offen
#1,185 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
bug module/aspnetcore-support needs-investigation p2 queued
Vorherrschende Sprache
C#
Sterne
1.7k
Forks
503
Ø Merge
1 T. 18 Std.
Gemergte PRs (30 T.)
21

Beschreibung

### 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Beginnen Sie damit, das Problem mit der bereitgestellten Middleware unter Amazon.Lambda.AspNetCoreServer.Hosting unter Verwendung von Minimal API hosting und LambdaEventSource.RestApi auf .NET 6 zu reproduzieren. Vergleichen Sie die Verarbeitung des Response-Bodys mit Kestrel oder dem Integrationstestserver; als erledigt gilt die Aufgabe, wenn die AWS Lambda-Antwort den JSON-Body genau einmal enthält. In dem Bericht wird keine Repository-Datei und kein Test genannt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
aws, csharp
Bereich
backend, cloud
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.