-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
28 lines (20 loc) · 820 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using AspNetCoreChannels;
using System.Threading.Channels;
var builder = WebApplication.CreateBuilder(args);
// Setup the channel
builder.Services.AddSingleton(Channel.CreateUnbounded<Model>());
builder.Services.AddSingleton(p => p.GetRequiredService<Channel<Model>>().Reader);
builder.Services.AddSingleton(p => p.GetRequiredService<Channel<Model>>().Writer);
// Setup the hosted service
builder.Services.AddHostedService<ModelService>();
var app = builder.Build();
// Simple HTTP POST that receives an array of models
app.MapPost("/messages", async (ChannelWriter<Model> writer, ICollection<Model> models, CancellationToken cancellationToken) =>
{
foreach (var model in models)
{
await writer.WriteAsync(model, cancellationToken);
}
return StatusCodes.Status202Accepted;
});
app.Run();