Introduction

An ASP.NET Core API normally follows a familiar request and response model. A client sends a request, the API processes it, and the server returns a response.

That model becomes more interesting when the API is placed behind a streaming gateway.

Instead of waiting for the complete response, the gateway can forward data to the client as it becomes available. This is useful for large responses, long-running operations, real-time updates, AI responses, server-sent events, and other workloads where keeping the connection open is part of the application design.

However, adding a streaming gateway changes more than the network path.

Timeouts, buffering, response headers, cancellation, connection lifetime, error handling, and observability all become important.

A request that works correctly when the client communicates directly with ASP.NET Core can behave differently when traffic passes through a gateway.

This article explains what changes in the request flow and what developers should consider when running an ASP.NET Core API behind a streaming gateway.

Understanding the Request Path

Without a gateway, the request path may look like this:

Code
Client
   |
   v
ASP.NET Core API
   |
   v
Response

With a streaming gateway:

Code
Client
   |
   v
Streaming Gateway
   |
   v
ASP.NET Core API
   |
   v
Streaming Gateway
   |
   v
Client

The gateway now sits between the client and the application.

That means there are potentially multiple components involved in:

  • Connection establishment

  • Request forwarding

  • Authentication

  • Timeout handling

  • Response buffering

  • Connection termination

  • Retry behavior

  • Error handling

For a normal short-lived API request, these differences may be barely noticeable.

For a streaming response, they can become critical.

What Is Response Streaming?

Response streaming means that the server sends data progressively instead of waiting until the entire response is ready.

For example, an API might generate several pieces of data:

Code
Chunk 1
Chunk 2
Chunk 3
Chunk 4

Instead of returning:

Code
Complete response

the API can send:

Code
Chunk 1 → client
Chunk 2 → client
Chunk 3 → client
Chunk 4 → client

This reduces the time before the client receives the first useful data.

It is particularly useful for:

  • Large files

  • Long-running processing

  • AI-generated responses

  • Server-sent events

  • Progress updates

  • Log streams

  • Real-time application data

A Simple ASP.NET Core Streaming Endpoint

ASP.NET Core supports asynchronous response streaming.

For example:

C#
app.MapGet("/stream", async (
    HttpResponse response,
    CancellationToken cancellationToken) =>
{
    response.ContentType = "text/plain";

    for (var i = 1; i <= 10; i++)
    {
        await response.WriteAsync(
            $"Message {i}\n",
            cancellationToken);

        await response.Body.FlushAsync(cancellationToken);

        await Task.Delay(
            TimeSpan.FromSeconds(1),
            cancellationToken);
    }
});

The endpoint writes a message, flushes the response, waits, and continues.

When the client connects directly to the application, the behavior is relatively easy to understand.

Once a gateway is introduced, the gateway must also support forwarding the streamed response correctly.

The Gateway Can Change the Streaming Behavior

One of the most common issues is buffering.

Suppose ASP.NET Core produces:

Code
Chunk 1

and immediately flushes it.

The developer may expect the client to receive that chunk immediately.

However, if the gateway buffers the response, the client might receive:

Code
Chunk 1
Chunk 2
Chunk 3
Chunk 4

all at once.

The API is still technically streaming.

The end-to-end system is not.

This distinction is important:

Code
Application streaming
        ≠
End-to-end streaming

Every component between the producer and consumer needs to preserve the intended behavior.

Why Buffering Matters

Consider an AI response that generates one token group every few hundred milliseconds.

Without buffering:

Code
Gateway
   ↓
Token 1
Token 2
Token 3
Token 4

The user sees the response gradually.

With buffering:

Code
Gateway
   ↓
Wait
   ↓
Token 1 + Token 2 + Token 3 + Token 4

The user may see a long pause followed by a large block of output.

This can make a streaming application feel slow even when the backend is producing data quickly.

Configure the Response Correctly

The response headers should accurately describe the streaming behavior.

For example:

C#
app.MapGet("/events", async (
    HttpResponse response,
    CancellationToken cancellationToken) =>
{
    response.ContentType = "text/event-stream";

    for (var i = 1; i <= 5; i++)
    {
        await response.WriteAsync(
            $"data: Event {i}\n\n",
            cancellationToken);

        await response.Body.FlushAsync(cancellationToken);

        await Task.Delay(
            TimeSpan.FromSeconds(1),
            cancellationToken);
    }
});

The text/event-stream content type is commonly used for Server-Sent Events.

The gateway must preserve the response correctly rather than converting the response into a normal buffered response.

Streaming Does Not Mean Infinite Connections

A streaming endpoint may keep a connection open for a long time, but that does not mean the connection should remain open indefinitely.

You should define expected behavior for:

  • Maximum connection duration

  • Idle timeout

  • Request timeout

  • Gateway timeout

  • Backend timeout

  • Client cancellation

For example, an API may take several minutes to generate a response.

If the gateway has a 60-second timeout, the API may continue processing while the client receives a gateway timeout.

That creates an important operational problem.

Understand Timeout Layers

With a gateway in the middle, there may be several timeout settings:

Code
Client timeout
      |
      v
Gateway timeout
      |
      v
ASP.NET Core timeout
      |
      v
Downstream service timeout

These values should be designed intentionally.

For example:

Code
Client:     5 minutes
Gateway:    4 minutes
API:        3 minutes
Database:   2 minutes

This provides a predictable timeout hierarchy.

The exact values depend on the workload.

The important point is that the gateway timeout cannot be treated as an unrelated infrastructure setting.

Streaming Changes Cancellation Handling

Client cancellation becomes especially important for streaming APIs.

Suppose a user starts an AI request and closes the browser.

The client connection disappears.

The gateway may detect that the client has disconnected.

ASP.NET Core should also stop unnecessary backend work when cancellation reaches the application.

The CancellationToken passed to the endpoint can help:

C#
app.MapGet("/stream", async (
    HttpResponse response,
    CancellationToken cancellationToken) =>
{
    response.ContentType = "text/plain";

    while (!cancellationToken.IsCancellationRequested)
    {
        await response.WriteAsync(
            "Processing...\n",
            cancellationToken);

        await response.Body.FlushAsync(
            cancellationToken);

        await Task.Delay(
            TimeSpan.FromSeconds(1),
            cancellationToken);
    }
});

The goal is to avoid continuing expensive processing after the client no longer needs the result.

This is especially important when the API is performing:

  • AI inference

  • Database queries

  • External API calls

  • File processing

  • Large computations

Do Not Swallow Cancellation Exceptions

Streaming applications should distinguish between genuine application failures and expected cancellation.

For example:

C#
try
{
    await ProcessStreamAsync(cancellationToken);
}
catch (OperationCanceledException)
{
    // Client or request cancellation.
}

Cancellation should generally not be treated as an unexpected server failure.

Otherwise, logs can become filled with errors caused by normal client disconnects.

Authentication Still Happens at the API

A gateway may handle authentication or token validation, but the API should understand what security guarantees the gateway provides.

For example:

Code
Client
   |
   | Authorization
   v
Gateway
   |
   | Forward request
   v
ASP.NET Core API

Decide explicitly whether:

  • The gateway validates the token.

  • The API validates the token.

  • Both validate the token.

  • The gateway forwards identity information.

  • The API trusts specific gateway headers.

Do not automatically trust headers such as:

Code
X-User-Id
X-User-Role
X-Authenticated-User

unless the network architecture guarantees that clients cannot directly inject or modify them.

Security boundaries should be explicit.

Preserve Important Headers

A streaming gateway should preserve headers that affect how the response is interpreted.

Depending on the protocol and gateway, this may include:

Code
Content-Type
Cache-Control
Transfer-Encoding
Content-Encoding
Authorization

The exact header behavior depends on the gateway and protocol.

Developers should test the complete request path rather than assuming every header will pass through unchanged.

Caching Requires Extra Attention

Streaming responses are often not suitable for traditional response caching.

For example, an AI response generated for one user should not accidentally be cached and returned to another user.

If a response should not be cached, configure the appropriate cache behavior.

For example:

C#
response.Headers.CacheControl = "no-cache";

For sensitive or user-specific streaming content, caching should be evaluated carefully at every layer.

Compression Can Affect Streaming

Compression can improve network efficiency, but it can also interact with buffering.

If the response is compressed, data may not be transmitted exactly when the application calls FlushAsync.

The gateway may also decompress and recompress the response.

For latency-sensitive streaming applications, test:

Code
No compression
        vs
Compression

and measure time-to-first-byte and time between chunks.

Do not assume that compression always improves the user experience.

Streaming Large Responses

Streaming is particularly useful for large payloads.

A traditional API might build the entire response in memory:

C#
var result = BuildLargeResponse();

return Results.Ok(result);

For large data, this can increase memory usage.

Streaming allows the application to process and send smaller portions:

C#
await response.WriteAsync(chunk, cancellationToken);
await response.Body.FlushAsync(cancellationToken);

This can reduce memory pressure.

However, streaming does not automatically make the application memory-efficient.

If the application builds the complete dataset in memory before streaming it, the memory problem still exists.

For example:

Code
Database
   |
   v
Entire dataset in memory
   |
   v
Streaming response

is not true memory-efficient streaming.

A better design is:

Code
Database
   |
   v
Read batch
   |
   v
Write chunk
   |
   v
Read next batch

Error Handling Becomes More Complicated

Normal HTTP APIs commonly return an error response such as:

Code
HTTP/1.1 500 Internal Server Error
Content-Type: application/json

Streaming responses are different.

Once response data has already been sent, changing the HTTP status code may no longer be possible.

For example:

Code
HTTP 200
   |
   +-- Chunk 1
   +-- Chunk 2
   +-- Error occurs

At this point, the server cannot simply change the already-sent response to HTTP 500.

The application needs a protocol-level way to communicate errors.

For Server-Sent Events, this might mean sending an event describing the error:

Code
event: error
data: Processing failed

The client must understand that event.

This is one reason streaming protocols should define error semantics before implementation.

Retries Are Not Always Safe

A gateway may retry some failed requests depending on its configuration.

Retries can be useful for idempotent operations.

They can be dangerous for streaming or state-changing operations.

For example:

Code
POST /generate-report

might start an expensive job.

If the gateway automatically retries the request, the operation could run twice.

Streaming requests therefore require careful retry policies.

Ask:

  • Is the operation idempotent?

  • Can processing safely restart?

  • Can duplicate work occur?

  • Has response data already been sent?

  • Can the client safely reconnect?

Do not apply generic retry policies to streaming endpoints without testing their behavior.

Observability Needs to Span the Gateway

When a streaming request fails, developers need to know where the failure occurred.

A useful trace might look like:

Code
Client
  |
  | Request ID: 8f23
  v
Gateway
  |
  | Request ID: 8f23
  v
ASP.NET Core API
  |
  v
Database

Use correlation or trace identifiers consistently across the request path.

Record useful information such as:

  • Request ID

  • Trace ID

  • Endpoint

  • Request duration

  • Time to first byte

  • Stream duration

  • Bytes sent

  • Cancellation reason

  • Gateway status

  • Backend status

Avoid logging sensitive response content simply because the request is being streamed.

Measure Time to First Byte

For streaming APIs, total request duration is not the only useful metric.

Consider:

Code
Request duration: 30 seconds
Time to first byte: 200 ms

This may be perfectly acceptable for a streaming application.

Compare that with:

Code
Request duration: 30 seconds
Time to first byte: 25 seconds

The total duration is the same, but the user experience is very different.

Useful streaming metrics include:

  • Time to first byte

  • Time between chunks

  • Total stream duration

  • Disconnect rate

  • Gateway timeout rate

  • Backend cancellation rate

  • Response size

Troubleshooting Common Problems

The API streams but the client receives everything at once

Check for buffering in the gateway, proxy, load balancer, or compression layer.

Verify that the gateway supports streaming for the protocol being used.

The gateway returns a timeout

Compare the gateway timeout with the expected stream duration.

Also check whether the connection is considered idle when no data is being sent.

Sending periodic heartbeat data may be appropriate for some streaming protocols.

The backend continues processing after the client disconnects

Verify that request cancellation reaches the ASP.NET Core application.

Make sure downstream operations also accept the cancellation token.

HTTP status code cannot be changed after an error

This is expected once response headers have already been sent.

Define an application-level error event or protocol-specific error mechanism.

Streaming works locally but fails in production

Local testing may bypass the gateway.

Reproduce the complete production path:

Code
Client
→ Gateway
→ Load Balancer
→ ASP.NET Core
→ Downstream Services

Each layer can introduce buffering, timeout, compression, or connection-management behavior.

A Practical Configuration Checklist

Before putting a streaming ASP.NET Core API behind a gateway, verify:

  1. The gateway supports the required streaming protocol.

  2. Response buffering behavior is understood.

  3. Gateway timeout values are compatible with the workload.

  4. Client cancellation reaches ASP.NET Core.

  5. Downstream operations accept cancellation tokens.

  6. Required response headers are preserved.

  7. Compression behavior has been tested.

  8. Caching is disabled or configured appropriately.

  9. Retry policies are safe for the endpoint.

  10. Authentication boundaries are clearly defined.

  11. Error handling works after response streaming begins.

  12. Correlation IDs are preserved.

  13. Time-to-first-byte is monitored.

  14. The complete production network path has been tested.

Best Practices

Design for the complete request path

Do not design streaming behavior only inside the ASP.NET Core application.

The gateway is part of the application path and should be included in architecture and testing decisions.

Set timeout values intentionally

Use a timeout hierarchy rather than allowing each infrastructure component to use unrelated defaults.

Propagate cancellation

Pass the request cancellation token to database calls, HTTP calls, asynchronous operations, and long-running processing.

Avoid unnecessary buffering

If low-latency streaming is important, identify every layer that may buffer response data.

Define streaming errors

Once a response has started, normal HTTP error responses may no longer be available.

Use a protocol-level error mechanism where necessary.

Measure streaming-specific metrics

Time-to-first-byte and disconnect rates can be more useful than total request duration alone.

Advantages and Disadvantages

Area

Advantages

Considerations

User experience

Data can appear before processing finishes

Buffering can remove the benefit

Large responses

Reduces the need to hold the entire response in memory

Application still needs proper data streaming

AI workloads

Supports incremental generated responses

Long-running connections need timeout planning

Scalability

Can reduce unnecessary buffering

Many open connections consume resources

Cancellation

Work can stop when clients disconnect

Cancellation must propagate through dependencies

Architecture

Gateway can centralize traffic management

Adds another layer that can change behavior

Observability

Gateway provides another monitoring point

Troubleshooting requires end-to-end tracing

Conclusion

Running an ASP.NET Core API behind a streaming gateway changes the application architecture in ways that are easy to overlook.

The API may already support streaming correctly, but the complete request path can still introduce buffering, timeouts, compression, retries, authentication differences, or connection-management issues.

The most important design principle is to treat streaming as an end-to-end behavior.

The ASP.NET Core application, gateway, load balancer, client, and downstream services all need to agree on how the connection behaves.

If the application supports cancellation, the gateway preserves streaming correctly, timeout values are intentional, errors are handled at the protocol level, and streaming-specific metrics are monitored, ASP.NET Core APIs can work reliably behind a streaming gateway without turning every long-running request into an infrastructure troubleshooting exercise.