Skip to content

How to properly implement Server-Sent Events with ASP.NET? #62839

@adamsitnik

Description

@adamsitnik

I am back from the leave and trying to catch up with most recent changes in the A2A project. While doing that, I've focused on exception handling.

I've noticed that A2AEventStreamResult.A2AEventStreamResult does not perform any exception handling.

https://github.com/a2aproject/a2a-dotnet/blob/47061171e14510b0b9676b2f26095963436ee1e0/src/A2A.AspNetCore/A2AHttpProcessor.cs#L412-L432

Since it was the first time I was using SSE ever, I've asked Copilot about the best practices:

Image

It has suggested to add a bunch of headers and I can see that some of our repos use it:

https://github.com/dotnet/WatsonWebserver/blob/d3d1c5d3f93ea3be6dda3b56a2da2b6977078b2c/src/WatsonWebserver/HttpResponse.cs#L410-L412

context.Response.ContentType = "text/event-stream";
context.Response.Headers.CacheControl = "no-cache,no-store";
context.Response.Headers.Pragma = "no-cache";
// Make sure we disable all response buffering for SSE
var bufferingFeature = context.Features.GetRequiredFeature<IHttpResponseBodyFeature>();
bufferingFeature.DisableBuffering();

@DamianEdwards should we also do that? And moreover, should we also ignore OperationCanceledException and treat it as end of streaming as suggested by LLM? And emit error and close events as it suggested below?

Image

@DamianEdwards I was not able to find any high quality docs about best practices for that which makes it impossible for me to say whether LLM is hallucinating or telling the truth.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automaticallyquestion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions