From 8cf446a07a291b56581c0864339dd3ea8b5b893d Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Fri, 4 Apr 2025 17:10:07 -1000 Subject: [PATCH 01/11] SSE return types /2 --- .../HeartRateRecord.cs | 4 ++ .../MinimalServerSentEvents/Program.cs | 8 +-- .../aspnetcore-10/includes/sse.md | 16 +++++ .../10/ControllerSSE/ControllerSSE.csproj | 13 ++++ .../10/ControllerSSE/ControllerSSE.http | 15 ++++ .../Controllers/HeartRateController.cs | 68 +++++++++++++++++++ .../samples/10/ControllerSSE/HearRate.cs | 4 ++ .../samples/10/ControllerSSE/Program.cs | 28 ++++++++ 8 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/HeartRateRecord.cs create mode 100644 aspnetcore/release-notes/aspnetcore-10/includes/sse.md create mode 100644 aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.csproj create mode 100644 aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.http create mode 100644 aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Controllers/HeartRateController.cs create mode 100644 aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs create mode 100644 aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Program.cs diff --git a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/HeartRateRecord.cs b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/HeartRateRecord.cs new file mode 100644 index 000000000000..b9950900f083 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/HeartRateRecord.cs @@ -0,0 +1,4 @@ +public record HeartRateRecord(DateTime Timestamp, int HeartRate) +{ + public static HeartRateRecord Create(int heartRate) => new(DateTime.UtcNow, heartRate); +} diff --git a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs index 42b873277604..af2a801028e2 100644 --- a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs @@ -25,13 +25,13 @@ async IAsyncEnumerable GetHeartRate( // app.MapGet("/json-item", (CancellationToken cancellationToken) => { - async IAsyncEnumerable GetHeartRate( + async IAsyncEnumerable GetHeartRate( [EnumeratorCancellation] CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { var heartRate = Random.Shared.Next(60, 100); - yield return HearRate.Create(heartRate); + yield return HeartRateRecord.Create(heartRate); await Task.Delay(2000, cancellationToken); } } @@ -64,7 +64,3 @@ async IAsyncEnumerable> GetHeartRate( app.Run(); -public record HearRate(DateTime Timestamp, int HeartRate) -{ - public static HearRate Create(int heartRate) => new(DateTime.UtcNow, heartRate); -} diff --git a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md new file mode 100644 index 000000000000..78c5ac6b0341 --- /dev/null +++ b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md @@ -0,0 +1,16 @@ +### Support for Server-Sent Events (SSE) + +ASP.NET Core now supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result using the [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API. This feature is supported in both Minimal APIs and controller-based apps. + +Server-Sent Events is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. + +The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a new static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. + +The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: + +:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_json" ::: + +For more information, see: + +- +- [Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) on MDN. \ No newline at end of file diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.csproj b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.csproj new file mode 100644 index 000000000000..b6fe0f1ed00c --- /dev/null +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.csproj @@ -0,0 +1,13 @@ + + + + net10.0 + enable + enable + + + + + + + diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.http b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.http new file mode 100644 index 000000000000..960c3051d23b --- /dev/null +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/ControllerSSE.http @@ -0,0 +1,15 @@ +@baseUrl = http://localhost:5201/HeartRate + +### Connect to SSE stream +# This request will open an SSE connection that stays open +GET {{baseUrl}}/string-item +Accept: text/event-stream + +### +GET {{baseUrl}}/json-item +Accept: text/event-stream + +### + +GET {{baseUrl}}/sse-item +Accept: text/event-stream \ No newline at end of file diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Controllers/HeartRateController.cs b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Controllers/HeartRateController.cs new file mode 100644 index 000000000000..f42f9a57c468 --- /dev/null +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Controllers/HeartRateController.cs @@ -0,0 +1,68 @@ +using Microsoft.AspNetCore.Mvc; +using System.Runtime.CompilerServices; +using System.Net.ServerSentEvents; + +[ApiController] +[Route("[controller]")] + +public class HeartRateController : ControllerBase +{ + // /HeartRate/json-item + [HttpGet("json-item")] + public IResult GetHeartRateJson(CancellationToken cancellationToken) + { + async IAsyncEnumerable StreamHeartRates( + [EnumeratorCancellation] CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + var heartRate = Random.Shared.Next(60, 100); + yield return HearRate.Create(heartRate); + await Task.Delay(2000, cancellationToken); + } + } + + return TypedResults.ServerSentEvents(StreamHeartRates(cancellationToken), eventType: "heartRate"); + } + + // /HeartRate/string-item + [HttpGet("string-item")] + + public IResult GetHeartRateString(CancellationToken cancellationToken) + { + async IAsyncEnumerable GetHeartRate( + [EnumeratorCancellation] CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + var heartRate = Random.Shared.Next(60, 100); + yield return $"Hear Rate: {heartRate} bpm"; + await Task.Delay(2000, cancellationToken); + } + } + + return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken), eventType: "heartRate"); + } + + // /HeartRate/sse-item + [HttpGet("sse-item")] + + public IResult GetHeartRateSSE(CancellationToken cancellationToken) + { + async IAsyncEnumerable> GetHeartRate( + [EnumeratorCancellation] CancellationToken cancellationToken) + { + while (!cancellationToken.IsCancellationRequested) + { + var heartRate = Random.Shared.Next(60, 100); + yield return new SseItem(heartRate, eventType: "heartRate") + { + ReconnectionInterval = TimeSpan.FromMinutes(1) + }; + await Task.Delay(2000, cancellationToken); + } + } + + return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken)); + } +} diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs new file mode 100644 index 000000000000..f5a692a9f856 --- /dev/null +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs @@ -0,0 +1,4 @@ +public record HearRate(DateTime Timestamp, int HeartRate) +{ + public static HearRate Create(int heartRate) => new(DateTime.UtcNow, heartRate); +} diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Program.cs b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Program.cs new file mode 100644 index 000000000000..08ee54a07da9 --- /dev/null +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/Program.cs @@ -0,0 +1,28 @@ + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi +builder.Services.AddOpenApi(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} + +app.UseHttpsRedirection(); + +app.UseAuthorization(); + + +app.MapControllers(); + +app.MapGet("/", () => Results.Redirect("/HeartRate/json-item")); + + +app.Run(); \ No newline at end of file From 8b46ddecb3c9a02895f93478c075f77437513230 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 7 Apr 2025 10:02:19 -1000 Subject: [PATCH 02/11] SSE return types /2 --- .../MinimalServerSentEvents/MinimalServerSentEvents.http | 4 ++-- aspnetcore/release-notes/aspnetcore-10.0.md | 3 +++ aspnetcore/release-notes/aspnetcore-10/includes/sse.md | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/MinimalServerSentEvents.http b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/MinimalServerSentEvents.http index 6e19822ba6a5..b61db15b6b30 100644 --- a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/MinimalServerSentEvents.http +++ b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/MinimalServerSentEvents.http @@ -1,4 +1,4 @@ -@baseUrl = http://localhost:5293 +@baseUrl = http://localhost:58489 ### Connect to SSE stream # This request will open an SSE connection that stays open @@ -12,4 +12,4 @@ Accept: text/event-stream ### GET {{baseUrl}}/sse-item -Accept: text/event-stream \ No newline at end of file +Accept: text/event-stream diff --git a/aspnetcore/release-notes/aspnetcore-10.0.md b/aspnetcore/release-notes/aspnetcore-10.0.md index cefb6b31014e..d158c00283d0 100644 --- a/aspnetcore/release-notes/aspnetcore-10.0.md +++ b/aspnetcore/release-notes/aspnetcore-10.0.md @@ -37,6 +37,9 @@ This section describes new features for minimal APIs. [!INCLUDE[](~/release-notes/aspnetcore-10/includes/MinApiEmptyStringInFormPost.md)] +[!INCLUDE[](~/release-notes/aspnetcore-10/includes/sse.md)] + + ## OpenAPI This section describes new features for OpenAPI. diff --git a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md index 78c5ac6b0341..7e249c0f31f4 100644 --- a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md +++ b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md @@ -13,4 +13,5 @@ The following example illustrates how to use the `TypedResults.ServerSentEvents For more information, see: - -- [Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) on MDN. \ No newline at end of file +- [Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) on MDN. +- \ No newline at end of file From 78059a965ec9c84eb4c02d1506345dc9817021ca Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 7 Apr 2025 13:25:29 -1000 Subject: [PATCH 03/11] SSE return types /2 --- .../10.0-samples/MinimalServerSentEvents/Program.cs | 2 +- aspnetcore/release-notes/aspnetcore-10.0.md | 1 - aspnetcore/release-notes/aspnetcore-10/includes/sse.md | 8 ++++---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs index af2a801028e2..be40ac1443d3 100644 --- a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs @@ -13,7 +13,7 @@ async IAsyncEnumerable GetHeartRate( while (!cancellationToken.IsCancellationRequested) { var heartRate = Random.Shared.Next(60, 100); - yield return $"Hear Rate: {heartRate} bpm"; + yield return $"Heart Rate: {heartRate} bpm"; await Task.Delay(2000, cancellationToken); } } diff --git a/aspnetcore/release-notes/aspnetcore-10.0.md b/aspnetcore/release-notes/aspnetcore-10.0.md index d158c00283d0..941232cc039a 100644 --- a/aspnetcore/release-notes/aspnetcore-10.0.md +++ b/aspnetcore/release-notes/aspnetcore-10.0.md @@ -39,7 +39,6 @@ This section describes new features for minimal APIs. [!INCLUDE[](~/release-notes/aspnetcore-10/includes/sse.md)] - ## OpenAPI This section describes new features for OpenAPI. diff --git a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md index 7e249c0f31f4..a608f81847ff 100644 --- a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md +++ b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md @@ -6,12 +6,12 @@ Server-Sent Events is a server push technology that allows a server to send a st The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a new static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. -The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: +The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: :::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_json" ::: For more information, see: -- -- [Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) on MDN. -- \ No newline at end of file +- [Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) on MDN. +- [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. +- [Controller API sample app](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. From d4454bc7bec1861e9daf030a10670e2ad11aaf48 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 7 Apr 2025 13:40:26 -1000 Subject: [PATCH 04/11] SSE return types /2 --- .../fundamentals/minimal-apis/responses.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 0f6dca09bb47..bf217e1e5647 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -120,7 +120,7 @@ In order to document this endpoint correctly the extension method `Produces` is :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_04"::: - + ### Built-in results @@ -128,6 +128,20 @@ In order to document this endpoint correctly the extension method `Produces` is The following sections demonstrate the usage of the common result helpers. +#### Server-Sent Events (SSE) + +The [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result. + +[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. + +The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. + +The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: + +:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_json" ::: + +For more information, see the [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. + #### JSON :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_05"::: @@ -150,6 +164,8 @@ The preceding example returns a 500 status code. :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_12"::: +#### Problem + #### Text :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08"::: From 9c968a1127c6bd6da040d9ffa74fda40677ea440 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Mon, 7 Apr 2025 14:17:34 -1000 Subject: [PATCH 05/11] SSE return types /2 --- .../10.0-samples/MinimalServerSentEvents/Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs index be40ac1443d3..d3354b692fc1 100644 --- a/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs @@ -18,7 +18,8 @@ async IAsyncEnumerable GetHeartRate( } } - return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken), eventType: "heartRate"); + return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken), + eventType: "heartRate"); }); // @@ -36,7 +37,8 @@ async IAsyncEnumerable GetHeartRate( } } - return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken), eventType: "heartRate"); + return TypedResults.ServerSentEvents(GetHeartRate(cancellationToken), + eventType: "heartRate"); }); // From afedaef7ddef4b00b64bd664720a420a8bbef638 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:07:47 -1000 Subject: [PATCH 06/11] fixes --- aspnetcore/release-notes/aspnetcore-10/includes/sse.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md index a608f81847ff..dd48c54d8340 100644 --- a/aspnetcore/release-notes/aspnetcore-10/includes/sse.md +++ b/aspnetcore/release-notes/aspnetcore-10/includes/sse.md @@ -2,7 +2,7 @@ ASP.NET Core now supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result using the [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API. This feature is supported in both Minimal APIs and controller-based apps. -Server-Sent Events is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. +Server-Sent Events is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a new static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. From 4afb5de41d2dc3a861c41cabbe9462357a1aba05 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:38:19 -1000 Subject: [PATCH 07/11] Update aspnetcore/fundamentals/minimal-apis/responses.md Co-authored-by: Mike Kistler --- aspnetcore/fundamentals/minimal-apis/responses.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index bf217e1e5647..115a74e0dc8b 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -132,7 +132,7 @@ The following sections demonstrate the usage of the common result helpers. The [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result. -[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. +[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. From 70c6b079956ef5762fba502d5ff108c610219f79 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:40:05 -1000 Subject: [PATCH 08/11] Apply suggestions from code review Co-authored-by: Mike Kistler --- aspnetcore/fundamentals/minimal-apis/responses.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 115a74e0dc8b..25eff7478b53 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -138,7 +138,7 @@ The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a stat The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: -:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_json" ::: +:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_item" ::: For more information, see the [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. From 732f590bedba8398c76201cf439eac7e6ade0e09 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 12:43:32 -1000 Subject: [PATCH 09/11] react to feedback --- .../fundamentals/minimal-apis/responses.md | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 25eff7478b53..0e72e510456e 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -128,20 +128,6 @@ In order to document this endpoint correctly the extension method `Produces` is The following sections demonstrate the usage of the common result helpers. -#### Server-Sent Events (SSE) - -The [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result. - -[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. - -The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. - -The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: - -:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_item" ::: - -For more information, see the [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. - #### JSON :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_05"::: @@ -188,6 +174,20 @@ The following example streams a video from an Azure Blob: [!code-csharp[](~/fundamentals/minimal-apis/resultsStream/7.0-samples/ResultsStreamSample/Program.cs?name=snippet_video)] +#### Server-Sent Events (SSE) + +The [TypedResults.ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,051e6796e1492f84) API supports returning a [ServerSentEvents](xref:System.Net.ServerSentEvents) result. + +[Server-Sent Events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) is a server push technology that allows a server to send a stream of event messages to a client over a single HTTP connection. In .NET, the event messages are represented as [`SseItem`](/dotnet/api/system.net.serversentevents.sseitem-1) objects, which may contain an event type, an ID, and a data payload of type `T`. + +The [TypedResults](xref:Microsoft.AspNetCore.Http.TypedResults) class has a static method called [ServerSentEvents](https://source.dot.net/#Microsoft.AspNetCore.Http.Results/TypedResults.cs,ceb980606eb9e295) that can be used to return a `ServerSentEvents` result. The first parameter to this method is an `IAsyncEnumerable>` that represents the stream of event messages to be sent to the client. + +The following example illustrates how to use the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as JSON objects to the client: + +:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs" id="snippet_item" ::: + +For more information, see the [Minimal API sample app](https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/MinimalServerSentEvents/Program.cs) using the `TypedResults.ServerSentEvents` API to return a stream of heart rate events as string, `ServerSentEvents`, and JSON objects to the client. + #### Redirect :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_09"::: From 979d782a530214e1334ce9137fedc44fe0df2c37 Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 14:26:08 -1000 Subject: [PATCH 10/11] Update aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs Co-authored-by: Mike Kistler --- .../action-return-types/samples/10/ControllerSSE/HearRate.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs index f5a692a9f856..c784313bc4ad 100644 --- a/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs +++ b/aspnetcore/web-api/action-return-types/samples/10/ControllerSSE/HearRate.cs @@ -1,4 +1,4 @@ -public record HearRate(DateTime Timestamp, int HeartRate) +public record HeartRate(DateTime Timestamp, int HeartRate) { - public static HearRate Create(int heartRate) => new(DateTime.UtcNow, heartRate); + public static HeartRate Create(int heartRate) => new(DateTime.UtcNow, heartRate); } From 1da8e86cb555f6c32c9acb615d958544c499688b Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Wed, 9 Apr 2025 14:28:39 -1000 Subject: [PATCH 11/11] react to feedback --- aspnetcore/fundamentals/minimal-apis/responses.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/responses.md b/aspnetcore/fundamentals/minimal-apis/responses.md index 0e72e510456e..71cb31cc663a 100644 --- a/aspnetcore/fundamentals/minimal-apis/responses.md +++ b/aspnetcore/fundamentals/minimal-apis/responses.md @@ -150,8 +150,6 @@ The preceding example returns a 500 status code. :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_12"::: -#### Problem - #### Text :::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_08":::