Skip to content

Commit 1b9508f

Browse files
authored
Dev (#42)
* - don't inject http context accessor, not needed * - add server sent events support to web result endpoints for .net 10 and greater * - add sample list books as streaming response * - remove default implicit conversion between WebResult<TValue> and WebResult - remove ToWebResult extension method from WebResult<TValue> - Add WebResultWithStreamingResponse as a simpler form of ServerSentEventsWebResult. WebResultEndpoints using this form has simpler HandleAsync method signature. - Add sample endpoint utilizing WebResultWithStreamingResponse * - add tests for web result endpoint sse and streaming response cases * - WebResultWithStreamingResponse for WebResultEndpoints has been changed to function similar to a Minimal API with IAsyncEnumerable<> response - notnull contraint has been lifted for TValue parameter in WebResultWithStreamingResponse and ServerSentEventsWebResult * - update deps to latest * - bump version
1 parent 7a364e3 commit 1b9508f

23 files changed

Lines changed: 510 additions & 32 deletions

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<PackageRequireLicenseAcceptance>True</PackageRequireLicenseAcceptance>
2121

22-
<Version>2.0.1</Version>
22+
<Version>2.0.2</Version>
2323
</PropertyGroup>
2424
</Project>

samples/ServiceEndpointClient/ServiceEndpointClient.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
10-
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="10.0.1" />
9+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.2" />
10+
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="10.0.2" />
1111
</ItemGroup>
1212

1313
<ItemGroup>

samples/ShowcaseWebApi/Extensions/WebApplicationBuilderExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public static WebApplicationBuilder AddSwagger(this WebApplicationBuilder builde
4343

4444
public static WebApplicationBuilder AddWebServices(this WebApplicationBuilder builder)
4545
{
46-
//builder.Services.AddHttpContextAccessor();
4746
builder.Services.AddProblemDetails(options =>
4847
{
4948
options.CustomizeProblemDetails = context =>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Net.ServerSentEvents;
2+
using System.Runtime.CompilerServices;
3+
using Microsoft.EntityFrameworkCore;
4+
using ModEndpoints;
5+
using ModEndpoints.Core;
6+
using ShowcaseWebApi.Data;
7+
using ShowcaseWebApi.Features.Books.Configuration;
8+
9+
namespace ShowcaseWebApi.Features.Books;
10+
11+
[MapToGroup<BooksV1RouteGroup>()]
12+
internal class ListBooksSse(ServiceDbContext db)
13+
: WebResultEndpointWithEmptyRequest<IAsyncEnumerable<SseItem<ListBooksResponseItem>>>
14+
{
15+
protected override void Configure(
16+
EndpointConfigurationBuilder builder,
17+
EndpointConfigurationContext configurationContext)
18+
{
19+
builder.MapGet("/listSse/")
20+
.Produces<SseItem<ListBooksResponseItem>>(contentType: "text/event-stream");
21+
}
22+
23+
protected override async Task<WebResult<IAsyncEnumerable<SseItem<ListBooksResponseItem>>>> HandleAsync(
24+
CancellationToken ct)
25+
{
26+
return WebResults.ServerSentEvents(GetBooks(ct));
27+
28+
async IAsyncEnumerable<ListBooksResponseItem> GetBooks(
29+
[EnumeratorCancellation] CancellationToken ct)
30+
{
31+
await foreach (var book in db.Books
32+
.Select(b => new ListBooksResponseItem(
33+
b.Id,
34+
b.Title,
35+
b.Author,
36+
b.Price))
37+
.AsAsyncEnumerable()
38+
.WithCancellation(ct))
39+
{
40+
await Task.Delay(1000, ct);
41+
yield return book;
42+
}
43+
}
44+
}
45+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Runtime.CompilerServices;
2+
using Microsoft.EntityFrameworkCore;
3+
using ModEndpoints;
4+
using ModEndpoints.Core;
5+
using ShowcaseWebApi.Data;
6+
using ShowcaseWebApi.Features.Books.Configuration;
7+
8+
namespace ShowcaseWebApi.Features.Books;
9+
10+
[MapToGroup<BooksV1RouteGroup>()]
11+
internal class ListBooksWithStreamingResponse(ServiceDbContext db)
12+
: WebResultEndpointWithEmptyRequest<IAsyncEnumerable<ListBooksResponseItem>>
13+
{
14+
protected override void Configure(
15+
EndpointConfigurationBuilder builder,
16+
EndpointConfigurationContext configurationContext)
17+
{
18+
builder.MapGet("/listWithStreamingResponse/");
19+
}
20+
21+
protected override async Task<WebResult<IAsyncEnumerable<ListBooksResponseItem>>> HandleAsync(
22+
CancellationToken ct)
23+
{
24+
return WebResults.WithStreamingResponse(GetBooks(ct));
25+
26+
async IAsyncEnumerable<ListBooksResponseItem> GetBooks(
27+
[EnumeratorCancellation] CancellationToken ct)
28+
{
29+
await foreach (var book in db.Books
30+
.Select(b => new ListBooksResponseItem(
31+
b.Id,
32+
b.Title,
33+
b.Author,
34+
b.Price))
35+
.AsAsyncEnumerable()
36+
.WithCancellation(ct))
37+
{
38+
await Task.Delay(1000, ct);
39+
yield return book;
40+
}
41+
}
42+
}
43+
}

samples/ShowcaseWebApi/ShowcaseWebApi.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.1" />
8+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.2" />
99
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.0" />
1010
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.1.1" />
11-
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
12-
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
11+
<PackageReference Include="Asp.Versioning.Http" Version="8.1.1" />
12+
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

samples/WeatherForecastWebApi/WeatherForecastWebApi.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.1" />
11-
<PackageReference Include="Scalar.AspNetCore" Version="2.11.10" />
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.2" />
11+
<PackageReference Include="Scalar.AspNetCore" Version="2.12.20" />
1212
</ItemGroup>
1313

1414
<ItemGroup>

src/ModEndpoints.Core/ModEndpoints.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
17+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>

src/ModEndpoints.RemoteServices.Contracts/ModEndpoints.RemoteServices.Contracts.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</ItemGroup>
1515

1616
<ItemGroup>
17-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
17+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102">
1818
<PrivateAssets>all</PrivateAssets>
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>

src/ModEndpoints.RemoteServices/ModEndpoints.RemoteServices.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@
1919
</ItemGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
22+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.102">
2323
<PrivateAssets>all</PrivateAssets>
2424
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2525
</PackageReference>
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.1" />
30-
<PackageReference Include="ModResults" Version="1.1.1" />
29+
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.2" />
30+
<PackageReference Include="ModResults" Version="1.2.0" />
3131
</ItemGroup>
3232

3333
<ItemGroup>

0 commit comments

Comments
 (0)