From 1b40ffd35467cf488fbfd8025f9da65235366ee7 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 01:48:22 +0100 Subject: [PATCH 01/39] created new project --- .../7.0-samples/7.0-samples.csproj | 10 ++++++++++ .../bindStreamPipeReader/7.0-samples/Program.cs | 6 ++++++ .../7.0-samples/appsettings.Development.json | 8 ++++++++ .../bindStreamPipeReader/7.0-samples/appsettings.json | 9 +++++++++ 4 files changed, 33 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj new file mode 100644 index 000000000000..abe8fe27f141 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj @@ -0,0 +1,10 @@ + + + + net7.0 + enable + enable + _7._0_samples + + + diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs new file mode 100644 index 000000000000..1760df1d28be --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs @@ -0,0 +1,6 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From d9e11e753ebbc294a3844150d40c99738b25b9ae Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 01:55:41 +0100 Subject: [PATCH 02/39] added channels to serve as queue --- .../7.0-samples/Program.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs index 1760df1d28be..2adf4354316d 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs @@ -1,6 +1,48 @@ +using System.Threading.Channels; +using BackGroundQueueService; var builder = WebApplication.CreateBuilder(args); +// create a channel to send data to the background queue +builder.Services.AddSingleton>((_)=>Channel.CreateUnbounded()); + +// create a background queue service +builder.Services.AddHostedService(); var app = builder.Build(); +app.Use(async (context, next) => +{ + await next(); + // read the stream from the request body + var reader = new StreamReader(context.Request.Body); + + //.. you could decide to do some logging here + +}); + app.MapGet("/", () => "Hello World!"); +// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' +app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => +{ + // create a rewindable stream to be able to reuse the body stream + var reusableStream = new MemoryStream(); + + // copy the request body to the reusable stream + await body.CopyToAsync(reusableStream); + + // reset the stream to the beginning + reusableStream.Position = 0; + + // send the stream to the background queue + await queue.Writer.WriteAsync(reusableStream); + + // reset the stream to the beginning + reusableStream.Position = 0; + + // set the response body to the reusable stream + req.Body = new MemoryStream(reusableStream.ToArray()); + + return "registered"; +}); + + app.Run(); From 7974f8628782fb884b86798dec9ce3cc71ca2c46 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 01:57:25 +0100 Subject: [PATCH 03/39] added a background service --- .../7.0-samples/BackgroundQueueService.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs new file mode 100644 index 000000000000..8af612df9729 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs @@ -0,0 +1,47 @@ +using System.Text.Json; +using System.Threading.Channels; + +namespace BackGroundQueueService; + +class BackGroundQueue : BackgroundService +{ + private Channel _queue; + + public BackGroundQueue(Channel queue) + { + _queue = queue; + } + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + + await foreach (var dataStream in _queue.Reader.ReadAllAsync()) + { + // reset the stream to the beginning + dataStream.Position = 0; + + var reader = new StreamReader(dataStream); + try + { + var Person = JsonSerializer.Deserialize(await reader.ReadToEndAsync())!; + Console.WriteLine($"{Person.Name} is {Person.Age} years and from {Person.Country}"); + // you could do something else with the data + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + + } + + + return; + + } +} + +class Person +{ + public string Name { get; set; } = String.Empty; + public int Age { get; set; } + public string Country { get; set; } = String.Empty; +} \ No newline at end of file From d6921a64574df21e9275914b49250d4ef0e5155a Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 01:59:51 +0100 Subject: [PATCH 04/39] created project --- .../PipeStreamToBackgroundQueue.csproj | 9 +++++++++ .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 6 ++++++ .../appsettings.Development.json | 8 ++++++++ .../PipeStreamToBackgroundQueue/appsettings.json | 9 +++++++++ 4 files changed, 32 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/PipeStreamToBackgroundQueue.csproj create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.Development.json create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.json diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/PipeStreamToBackgroundQueue.csproj b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/PipeStreamToBackgroundQueue.csproj new file mode 100644 index 000000000000..4c2bb77d0106 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/PipeStreamToBackgroundQueue.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs new file mode 100644 index 000000000000..1760df1d28be --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -0,0 +1,6 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); + +app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From 994cd82cc7016c0d3ad5c0f044ba2e874b0c35b6 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:01:58 +0100 Subject: [PATCH 05/39] added channels to serve as a queue --- .../PipeStreamToBackgroundQueue/Program.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 1760df1d28be..2adf4354316d 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -1,6 +1,48 @@ +using System.Threading.Channels; +using BackGroundQueueService; var builder = WebApplication.CreateBuilder(args); +// create a channel to send data to the background queue +builder.Services.AddSingleton>((_)=>Channel.CreateUnbounded()); + +// create a background queue service +builder.Services.AddHostedService(); var app = builder.Build(); +app.Use(async (context, next) => +{ + await next(); + // read the stream from the request body + var reader = new StreamReader(context.Request.Body); + + //.. you could decide to do some logging here + +}); + app.MapGet("/", () => "Hello World!"); +// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' +app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => +{ + // create a rewindable stream to be able to reuse the body stream + var reusableStream = new MemoryStream(); + + // copy the request body to the reusable stream + await body.CopyToAsync(reusableStream); + + // reset the stream to the beginning + reusableStream.Position = 0; + + // send the stream to the background queue + await queue.Writer.WriteAsync(reusableStream); + + // reset the stream to the beginning + reusableStream.Position = 0; + + // set the response body to the reusable stream + req.Body = new MemoryStream(reusableStream.ToArray()); + + return "registered"; +}); + + app.Run(); From f9f0df6bf5d8129affb4e46dd45eb6fa28a1c961 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:03:05 +0100 Subject: [PATCH 06/39] created background file --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs new file mode 100644 index 000000000000..e69de29bb2d1 From ad7f78939f1239335283817442ce4914b35b6aa3 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:04:25 +0100 Subject: [PATCH 07/39] added background service --- .../BackgroundQueueService.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index e69de29bb2d1..8af612df9729 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -0,0 +1,47 @@ +using System.Text.Json; +using System.Threading.Channels; + +namespace BackGroundQueueService; + +class BackGroundQueue : BackgroundService +{ + private Channel _queue; + + public BackGroundQueue(Channel queue) + { + _queue = queue; + } + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + + await foreach (var dataStream in _queue.Reader.ReadAllAsync()) + { + // reset the stream to the beginning + dataStream.Position = 0; + + var reader = new StreamReader(dataStream); + try + { + var Person = JsonSerializer.Deserialize(await reader.ReadToEndAsync())!; + Console.WriteLine($"{Person.Name} is {Person.Age} years and from {Person.Country}"); + // you could do something else with the data + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + + } + + + return; + + } +} + +class Person +{ + public string Name { get; set; } = String.Empty; + public int Age { get; set; } + public string Country { get; set; } = String.Empty; +} \ No newline at end of file From f51a27bb42ec738621951bf01ae688999e63f5b4 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:09:48 +0100 Subject: [PATCH 08/39] added curl comment --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 2adf4354316d..4d44381a59bb 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -20,7 +20,7 @@ app.MapGet("/", () => "Hello World!"); -// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' +// curl --request POST https://localhost:54535/register --header "Content-Type: application/json" --data-raw "{ \"Name\":\"Samson\", \"Age\": 23, \"Country\":\"Nigeria\" }" app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => { // create a rewindable stream to be able to reuse the body stream From be77f8f8a057809157a265312daf5784fd0c7eb1 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:10:02 +0100 Subject: [PATCH 09/39] added curl comment --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 4d44381a59bb..9d7b934eddd7 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -20,7 +20,7 @@ app.MapGet("/", () => "Hello World!"); -// curl --request POST https://localhost:54535/register --header "Content-Type: application/json" --data-raw "{ \"Name\":\"Samson\", \"Age\": 23, \"Country\":\"Nigeria\" }" +// curl --request POST https://localhost:{port}/register --header "Content-Type: application/json" --data-raw "{ \"Name\":\"Samson\", \"Age\": 23, \"Country\":\"Nigeria\" }" app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => { // create a rewindable stream to be able to reuse the body stream From 9b8c83742f8d449a8bbca296d7f6e040174307d6 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:10:30 +0100 Subject: [PATCH 10/39] removed some files --- .../7.0-samples/7.0-samples.csproj | 10 ---- .../7.0-samples/BackgroundQueueService.cs | 47 ------------------ .../7.0-samples/Program.cs | 48 ------------------- .../7.0-samples/appsettings.Development.json | 8 ---- .../7.0-samples/appsettings.json | 9 ---- .../7.0-samples/readme.txt | 1 - 6 files changed, 123 deletions(-) delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj deleted file mode 100644 index abe8fe27f141..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/7.0-samples.csproj +++ /dev/null @@ -1,10 +0,0 @@ - - - - net7.0 - enable - enable - _7._0_samples - - - diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs deleted file mode 100644 index 8af612df9729..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/BackgroundQueueService.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Text.Json; -using System.Threading.Channels; - -namespace BackGroundQueueService; - -class BackGroundQueue : BackgroundService -{ - private Channel _queue; - - public BackGroundQueue(Channel queue) - { - _queue = queue; - } - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - - await foreach (var dataStream in _queue.Reader.ReadAllAsync()) - { - // reset the stream to the beginning - dataStream.Position = 0; - - var reader = new StreamReader(dataStream); - try - { - var Person = JsonSerializer.Deserialize(await reader.ReadToEndAsync())!; - Console.WriteLine($"{Person.Name} is {Person.Age} years and from {Person.Country}"); - // you could do something else with the data - } - catch (Exception ex) - { - Console.WriteLine(ex.Message); - } - - } - - - return; - - } -} - -class Person -{ - public string Name { get; set; } = String.Empty; - public int Age { get; set; } - public string Country { get; set; } = String.Empty; -} \ No newline at end of file diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs deleted file mode 100644 index 2adf4354316d..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/Program.cs +++ /dev/null @@ -1,48 +0,0 @@ -using System.Threading.Channels; -using BackGroundQueueService; -var builder = WebApplication.CreateBuilder(args); -// create a channel to send data to the background queue -builder.Services.AddSingleton>((_)=>Channel.CreateUnbounded()); - -// create a background queue service -builder.Services.AddHostedService(); -var app = builder.Build(); - -app.Use(async (context, next) => -{ - await next(); - // read the stream from the request body - var reader = new StreamReader(context.Request.Body); - - //.. you could decide to do some logging here - -}); - -app.MapGet("/", () => "Hello World!"); - -// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' -app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => -{ - // create a rewindable stream to be able to reuse the body stream - var reusableStream = new MemoryStream(); - - // copy the request body to the reusable stream - await body.CopyToAsync(reusableStream); - - // reset the stream to the beginning - reusableStream.Position = 0; - - // send the stream to the background queue - await queue.Writer.WriteAsync(reusableStream); - - // reset the stream to the beginning - reusableStream.Position = 0; - - // set the response body to the reusable stream - req.Body = new MemoryStream(reusableStream.ToArray()); - - return "registered"; -}); - - -app.Run(); diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json deleted file mode 100644 index 0c208ae9181e..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json deleted file mode 100644 index 10f68b8c8b4f..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt deleted file mode 100644 index 655bdceab454..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Create sample code here then delete this file. \ No newline at end of file From dfd3537df97f4d81bc7a4f4688fad81b92382eef Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 02:26:16 +0100 Subject: [PATCH 11/39] blank lines added --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 8af612df9729..826cec7ec187 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -44,4 +44,4 @@ class Person public string Name { get; set; } = String.Empty; public int Age { get; set; } public string Country { get; set; } = String.Empty; -} \ No newline at end of file +} diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 9d7b934eddd7..34cb25418676 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -46,3 +46,4 @@ app.Run(); + From 293670bbcc0feb7773582f6bd9508bfee9551a9f Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 02:34:52 +0100 Subject: [PATCH 12/39] add suggestion to batch Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../PipeStreamToBackgroundQueue/Program.cs | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 34cb25418676..602f249e100b 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -1,49 +1,47 @@ using System.Threading.Channels; using BackGroundQueueService; var builder = WebApplication.CreateBuilder(args); -// create a channel to send data to the background queue -builder.Services.AddSingleton>((_)=>Channel.CreateUnbounded()); +// Create a channel to send data to the background queue. +builder.Services.AddSingleton>((_) => Channel.CreateUnbounded()); -// create a background queue service +// Create a background queue service. builder.Services.AddHostedService(); var app = builder.Build(); app.Use(async (context, next) => { - await next(); - // read the stream from the request body - var reader = new StreamReader(context.Request.Body); - - //.. you could decide to do some logging here - + await next(); + var reader = new StreamReader(context.Request.Body); + app.Logger.LogInformation("Reading the stream from the request body."); }); app.MapGet("/", () => "Hello World!"); -// curl --request POST https://localhost:{port}/register --header "Content-Type: application/json" --data-raw "{ \"Name\":\"Samson\", \"Age\": 23, \"Country\":\"Nigeria\" }" +// curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => { - // create a rewindable stream to be able to reuse the body stream + // Create a rewindable stream to be able to reuse the body stream. var reusableStream = new MemoryStream(); - - // copy the request body to the reusable stream + + // Copy the request body to the reusable stream. await body.CopyToAsync(reusableStream); - // reset the stream to the beginning - reusableStream.Position = 0; + // Reset the stream to the beginning. + reusableStream.Position = 0; - // send the stream to the background queue - await queue.Writer.WriteAsync(reusableStream); + // Send the stream to the background queue. + await queue.Writer.WriteAsync(reusableStream); - // reset the stream to the beginning - reusableStream.Position = 0; + // Reset the stream to the beginning. + reusableStream.Position = 0; - // set the response body to the reusable stream + // Set the response body to the reusable stream. req.Body = new MemoryStream(reusableStream.ToArray()); - return "registered"; + return "registered"; }); app.Run(); + From f01c8da6e0f5d90dcade266948470ec4d4ff5604 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 02:41:26 +0100 Subject: [PATCH 13/39] logger added Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 826cec7ec187..5dff0220aacf 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -5,11 +5,12 @@ namespace BackGroundQueueService; class BackGroundQueue : BackgroundService { - private Channel _queue; + private readonly ILogger _logger; - public BackGroundQueue(Channel queue) + public BackGroundQueue(Channel queue, ILogger logger) { _queue = queue; + _logger = logger; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { From 16bc5122dbb8faa3390e66192e87df8cb77ab6b2 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 02:41:46 +0100 Subject: [PATCH 14/39] logging information Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 5dff0220aacf..7b5cc507424e 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -24,7 +24,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) try { var Person = JsonSerializer.Deserialize(await reader.ReadToEndAsync())!; - Console.WriteLine($"{Person.Name} is {Person.Age} years and from {Person.Country}"); + _logger.LogInformation($"{Person.Name} is {Person.Age} years and from {Person.Country}"); // you could do something else with the data } catch (Exception ex) From c53de061e9bafac018f32da9b4a8c9c3aba64e34 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 02:42:12 +0100 Subject: [PATCH 15/39] log error Co-authored-by: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 7b5cc507424e..aa3f27e0691c 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -29,7 +29,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } catch (Exception ex) { - Console.WriteLine(ex.Message); + _logger.LogError(ex.Message); } } From 833ac81ff727bd68781c7fe66a2b6edb0f072def Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 16:29:53 +0100 Subject: [PATCH 16/39] Edit: CamelCasing Co-authored-by: David Fowler --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index aa3f27e0691c..698267f76339 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -1,7 +1,7 @@ using System.Text.Json; using System.Threading.Channels; -namespace BackGroundQueueService; +namespace BackgroundQueueService; class BackGroundQueue : BackgroundService { From 5b7fba65f1ef71ab5e60c8bfcdc7d9acbc12801d Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 16:30:19 +0100 Subject: [PATCH 17/39] CamelCasing Co-authored-by: David Fowler --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 698267f76339..fa54ed672d9c 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -3,7 +3,7 @@ namespace BackgroundQueueService; -class BackGroundQueue : BackgroundService +class BackgroundQueue : BackgroundService { private readonly ILogger _logger; From 6abd29f0e87af09979e941bc6e34fa13c6ad444b Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 16:35:53 +0100 Subject: [PATCH 18/39] Added some modifiers Co-authored-by: David Fowler --- .../BackgroundQueueService.cs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index fa54ed672d9c..33e6df47e061 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -20,23 +20,17 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) // reset the stream to the beginning dataStream.Position = 0; - var reader = new StreamReader(dataStream); try { - var Person = JsonSerializer.Deserialize(await reader.ReadToEndAsync())!; - _logger.LogInformation($"{Person.Name} is {Person.Age} years and from {Person.Country}"); + var person = JsonSerializer.Deserialize(dataStream)!; + _logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}"); // you could do something else with the data } catch (Exception ex) { _logger.LogError(ex.Message); } - } - - - return; - } } From f0bdd460c32c550115c90ce864199d75880a7fc1 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Fri, 15 Jul 2022 18:28:24 +0100 Subject: [PATCH 19/39] edit: cancellation token Co-authored-by: David Fowler --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 33e6df47e061..3450feda73f0 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -12,9 +12,9 @@ public BackGroundQueue(Channel queue, ILogger logger) _queue = queue; _logger = logger; } + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await foreach (var dataStream in _queue.Reader.ReadAllAsync()) { // reset the stream to the beginning From d880cfecb8675cb5df9e8fbf0a75e70594f0d0ff Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 20:41:48 +0100 Subject: [PATCH 20/39] edit: CamelCasing on backgroundservice --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 3450feda73f0..85ad5d836e93 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -1,10 +1,11 @@ using System.Text.Json; using System.Threading.Channels; -namespace BackgroundQueueService; +namespace BackGroundQueueService; -class BackgroundQueue : BackgroundService +class BackGroundQueue : BackgroundService { + private readonly Channel _queue; private readonly ILogger _logger; public BackGroundQueue(Channel queue, ILogger logger) From 0113f8a4167ace29150120c96da1823541d47607 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Fri, 15 Jul 2022 20:43:06 +0100 Subject: [PATCH 21/39] added cancellation token --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 85ad5d836e93..44f7c0afd8eb 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -16,7 +16,7 @@ public BackGroundQueue(Channel queue, ILogger logger) protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - await foreach (var dataStream in _queue.Reader.ReadAllAsync()) + await foreach (var dataStream in _queue.Reader.ReadAllAsync(stoppingToken)) { // reset the stream to the beginning dataStream.Position = 0; From 2de82f0dce506766d8c5985d9c5b9fa81efb1455 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 05:53:59 +0100 Subject: [PATCH 22/39] edit: changed casing Co-authored-by: David Fowler --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 602f249e100b..c00713e1b686 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -5,7 +5,7 @@ builder.Services.AddSingleton>((_) => Channel.CreateUnbounded()); // Create a background queue service. -builder.Services.AddHostedService(); +builder.Services.AddHostedService(); var app = builder.Build(); app.Use(async (context, next) => From 044b94f2066440eab1ca87fe2ad007aa39cc1ef4 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 05:56:20 +0100 Subject: [PATCH 23/39] changed casing of background --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 8 ++++---- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 44f7c0afd8eb..1ab2d6ecc81f 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -1,14 +1,14 @@ using System.Text.Json; using System.Threading.Channels; -namespace BackGroundQueueService; +namespace BackgroundQueueService; -class BackGroundQueue : BackgroundService +class BackgroundQueue : BackgroundService { private readonly Channel _queue; - private readonly ILogger _logger; + private readonly ILogger _logger; - public BackGroundQueue(Channel queue, ILogger logger) + public BackgroundQueue(Channel queue, ILogger logger) { _queue = queue; _logger = logger; diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index c00713e1b686..ea88c4cf7b2a 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -1,5 +1,5 @@ using System.Threading.Channels; -using BackGroundQueueService; +using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. builder.Services.AddSingleton>((_) => Channel.CreateUnbounded()); From 6235a5b8e877205d8ab1c03aadc94998a1171f4b Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:02:00 +0100 Subject: [PATCH 24/39] return status code 202 --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index ea88c4cf7b2a..ef0c67c93d8c 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -2,7 +2,7 @@ using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. -builder.Services.AddSingleton>((_) => Channel.CreateUnbounded()); +builder.Services.AddSingleton>((_) => Channel.CreateBounded(100)); // Create a background queue service. builder.Services.AddHostedService(); @@ -38,7 +38,7 @@ // Set the response body to the reusable stream. req.Body = new MemoryStream(reusableStream.ToArray()); - return "registered"; + return Results.Accepted(); }); From abb678e6fde6a8edd689da9badd2ca0bf6a98498 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:26:27 +0100 Subject: [PATCH 25/39] removed unnecessary middleware --- .../PipeStreamToBackgroundQueue/Program.cs | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index ef0c67c93d8c..7cd1d298ff12 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -8,17 +8,11 @@ builder.Services.AddHostedService(); var app = builder.Build(); -app.Use(async (context, next) => -{ - await next(); - var reader = new StreamReader(context.Request.Body); - app.Logger.LogInformation("Reading the stream from the request body."); -}); app.MapGet("/", () => "Hello World!"); // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' -app.MapPost("/register", async (Stream body, HttpRequest req, Channel queue) => +app.MapPost("/register", async (Stream body, Channel queue) => { // Create a rewindable stream to be able to reuse the body stream. var reusableStream = new MemoryStream(); @@ -32,16 +26,8 @@ // Send the stream to the background queue. await queue.Writer.WriteAsync(reusableStream); - // Reset the stream to the beginning. - reusableStream.Position = 0; - - // Set the response body to the reusable stream. - req.Body = new MemoryStream(reusableStream.ToArray()); - return Results.Accepted(); }); app.Run(); - - From 81067fb96bed56f1a52bcbc29300169c573d4ab6 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:40:41 +0100 Subject: [PATCH 26/39] changed from stream to byte --- .../BackgroundQueueService.cs | 13 ++++++------- .../PipeStreamToBackgroundQueue/Program.cs | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 1ab2d6ecc81f..c6a64c9f75b5 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -5,10 +5,10 @@ namespace BackgroundQueueService; class BackgroundQueue : BackgroundService { - private readonly Channel _queue; + private readonly Channel> _queue; private readonly ILogger _logger; - public BackgroundQueue(Channel queue, ILogger logger) + public BackgroundQueue(Channel> queue, ILogger logger) { _queue = queue; _logger = logger; @@ -18,13 +18,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await foreach (var dataStream in _queue.Reader.ReadAllAsync(stoppingToken)) { - // reset the stream to the beginning - dataStream.Position = 0; - + try { - var person = JsonSerializer.Deserialize(dataStream)!; - _logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}"); + + // var person = JsonSerializer.Deserialize(dataStream.Span); + // _logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}"); // you could do something else with the data } catch (Exception ex) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 7cd1d298ff12..6772f4871fdf 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -2,7 +2,7 @@ using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. -builder.Services.AddSingleton>((_) => Channel.CreateBounded(100)); +builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(100)); // Create a background queue service. builder.Services.AddHostedService(); @@ -12,7 +12,7 @@ app.MapGet("/", () => "Hello World!"); // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' -app.MapPost("/register", async (Stream body, Channel queue) => +app.MapPost("/register", async (Stream body, Channel> queue) => { // Create a rewindable stream to be able to reuse the body stream. var reusableStream = new MemoryStream(); From fa3f4547d21fd2f2da1416a801f5e4901d6b5ce4 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:45:06 +0100 Subject: [PATCH 27/39] changed channel type to memory --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 8 +++----- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 4 ++-- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index c6a64c9f75b5..62419d95cdf9 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -18,12 +18,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { await foreach (var dataStream in _queue.Reader.ReadAllAsync(stoppingToken)) { - try - { - - // var person = JsonSerializer.Deserialize(dataStream.Span); - // _logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}"); + { + var person = JsonSerializer.Deserialize(dataStream.Span)!; + _logger.LogInformation($"{person.Name} is {person.Age} years and from {person.Country}"); // you could do something else with the data } catch (Exception ex) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 6772f4871fdf..53f90aab93d3 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -24,8 +24,8 @@ reusableStream.Position = 0; // Send the stream to the background queue. - await queue.Writer.WriteAsync(reusableStream); - + await queue.Writer.WriteAsync(reusableStream.ToArray()); + return Results.Accepted(); }); From a70571ed10747e0d8aff5f3612149742cd144bae Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:46:00 +0100 Subject: [PATCH 28/39] removed unnecessary endpoint --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 53f90aab93d3..e736a42de042 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -9,8 +9,6 @@ var app = builder.Build(); -app.MapGet("/", () => "Hello World!"); - // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' app.MapPost("/register", async (Stream body, Channel> queue) => { From 528f021fe4c0e6d5e9340035fb4d825de2141176 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 06:51:28 +0100 Subject: [PATCH 29/39] remove line Co-authored-by: David Fowler --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index e736a42de042..06667c09c060 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -7,8 +7,6 @@ // Create a background queue service. builder.Services.AddHostedService(); var app = builder.Build(); - - // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' app.MapPost("/register", async (Stream body, Channel> queue) => { From 19ef786cea9b50e4f235dbecaf3d05fc4e2ddab3 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:53:33 +0100 Subject: [PATCH 30/39] changed from Memory to ReadOnlyMemory --- .../PipeStreamToBackgroundQueue/BackgroundQueueService.cs | 4 ++-- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs index 62419d95cdf9..4a271c5cbaec 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs @@ -5,10 +5,10 @@ namespace BackgroundQueueService; class BackgroundQueue : BackgroundService { - private readonly Channel> _queue; + private readonly Channel> _queue; private readonly ILogger _logger; - public BackgroundQueue(Channel> queue, ILogger logger) + public BackgroundQueue(Channel> queue, ILogger logger) { _queue = queue; _logger = logger; diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index e736a42de042..c2081e1e8575 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -2,7 +2,7 @@ using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. -builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(100)); +builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(100)); // Create a background queue service. builder.Services.AddHostedService(); @@ -10,7 +10,7 @@ // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' -app.MapPost("/register", async (Stream body, Channel> queue) => +app.MapPost("/register", async (Stream body, Channel> queue) => { // Create a rewindable stream to be able to reuse the body stream. var reusableStream = new MemoryStream(); @@ -18,9 +18,6 @@ // Copy the request body to the reusable stream. await body.CopyToAsync(reusableStream); - // Reset the stream to the beginning. - reusableStream.Position = 0; - // Send the stream to the background queue. await queue.Writer.WriteAsync(reusableStream.ToArray()); From 2e9f1e5f76b0322e88f9749378df05ae08a1e2ba Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:54:22 +0100 Subject: [PATCH 31/39] added a line --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 5f23694d5277..97487c24dff5 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -7,6 +7,7 @@ // Create a background queue service. builder.Services.AddHostedService(); var app = builder.Build(); + // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' app.MapPost("/register", async (Stream body, Channel> queue) => { From 072d6f6f041a4fca9009c71b961117535a1bc49f Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 06:57:30 +0100 Subject: [PATCH 32/39] use buffer to avoid double allocation Co-authored-by: David Fowler --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 97487c24dff5..4a8e28ed65ef 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -18,7 +18,8 @@ await body.CopyToAsync(reusableStream); // Send the stream to the background queue. - await queue.Writer.WriteAsync(reusableStream.ToArray()); + reusableStream.TryGetBuffer(out var buffer); + await queue.Writer.WriteAsync(buffer); return Results.Accepted(); }); From cb324573f6996ed97a77ce7a1d65fab7de50d54e Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:58:51 +0100 Subject: [PATCH 33/39] added a comment --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 4a8e28ed65ef..72383b914caa 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -17,9 +17,11 @@ // Copy the request body to the reusable stream. await body.CopyToAsync(reusableStream); + // get buffer to avoid double allocation + reusableStream.TryGetBuffer(out var buffer); + // Send the stream to the background queue. - reusableStream.TryGetBuffer(out var buffer); - await queue.Writer.WriteAsync(buffer); + await queue.Writer.WriteAsync(buffer); return Results.Accepted(); }); From 38a5ee748f2daebd87aaa1267d17ef23c5809ab0 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 06:59:23 +0100 Subject: [PATCH 34/39] added a comment --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 72383b914caa..8c9b4a3fbc39 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -19,8 +19,8 @@ // get buffer to avoid double allocation reusableStream.TryGetBuffer(out var buffer); - - // Send the stream to the background queue. + + // Send the buffer to the background queue. await queue.Writer.WriteAsync(buffer); return Results.Accepted(); From dba2da5f10e6a803daf66461ebef7627034384a2 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 07:13:08 +0100 Subject: [PATCH 35/39] reduced bounded size --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 8c9b4a3fbc39..4fc70d1f6bb5 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -2,7 +2,7 @@ using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. -builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(100)); +builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(20)); // Create a background queue service. builder.Services.AddHostedService(); @@ -19,7 +19,7 @@ // get buffer to avoid double allocation reusableStream.TryGetBuffer(out var buffer); - + // Send the buffer to the background queue. await queue.Writer.WriteAsync(buffer); From 5fdd2bd0949871e703e07467b54461a2a86915b2 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 07:37:37 +0100 Subject: [PATCH 36/39] add: spacing Co-authored-by: David Fowler --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 8c9b4a3fbc39..2ba202ec6491 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -1,5 +1,6 @@ using System.Threading.Channels; using BackgroundQueueService; + var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(100)); From 7311686caa444f216b215bf6d9d32101da3675fb Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 16 Jul 2022 07:45:05 +0100 Subject: [PATCH 37/39] increase bounded size --- .../7.0-samples/PipeStreamToBackgroundQueue/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 7ce9a6ae7434..0fcd60932433 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -3,7 +3,7 @@ var builder = WebApplication.CreateBuilder(args); // Create a channel to send data to the background queue. -builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(20)); +builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(6400)); // Create a background queue service. builder.Services.AddHostedService(); From 41f3c074e3ce2baeae64011880952369da6ce824 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 08:00:07 +0100 Subject: [PATCH 38/39] add bounded constraint Co-authored-by: David Fowler --- .../PipeStreamToBackgroundQueue/Program.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index 0fcd60932433..c9c9945863cd 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -2,8 +2,17 @@ using BackgroundQueueService; var builder = WebApplication.CreateBuilder(args); +// The max memory we're willing to use for the upload endpoint on this instance +var maxMemory = 500 * 1024 * 1024; + +// The max size of a single message, we're staying below the default LOH size of 85K +var maxMessageSize = 80 * 1024; + +// The max size of the queue based on those restrictions +var maxQueueSize = maxMemory / maxMessageSize; + // Create a channel to send data to the background queue. -builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(6400)); +builder.Services.AddSingleton>>((_) => Channel.CreateBounded>(maxQueueSize)); // Create a background queue service. builder.Services.AddHostedService(); From e247286bdb5598c5e13c77716ec53d032b719fc6 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Sat, 16 Jul 2022 08:22:41 +0100 Subject: [PATCH 39/39] confirming request-size Co-authored-by: David Fowler --- .../PipeStreamToBackgroundQueue/Program.cs | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs index c9c9945863cd..86f8f8d9e2c6 100644 --- a/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -19,21 +19,40 @@ var app = builder.Build(); // curl --request POST 'http://localhost:5256/register' --header 'Content-Type: application/json' --data-raw '{ "Name":"Samson", "Age": 23, "Country":"Nigeria" }' -app.MapPost("/register", async (Stream body, Channel> queue) => +app.MapPost("/register", async (HttpRequest req, Stream body, Channel> queue) => { - // Create a rewindable stream to be able to reuse the body stream. - var reusableStream = new MemoryStream(); - - // Copy the request body to the reusable stream. - await body.CopyToAsync(reusableStream); - - // get buffer to avoid double allocation - reusableStream.TryGetBuffer(out var buffer); - - // Send the buffer to the background queue. - await queue.Writer.WriteAsync(buffer); - - return Results.Accepted(); + if (req.ContentLength is not null && req.ContentLength > maxMessageSize) + { + // Message size exceeded + return Results.BadRequest(); + } + + // We're not above the message size and we have a content length, or + // we're a chunked request and we're going to read up to the maxMessageSize + 1. + // We add one to the message size so that we can detect when a chunked request body + // is bigger than our configured max. + var readSize = (int?)req.ContentLength ?? (maxMessageSize + 1); + + var buffer = new byte[readSize]; + + // Read at least that many bytes from the body + var read = await body.ReadAtLeastAsync(buffer, readSize, throwOnEndOfStream: false); + + // We read more than the max, so this is a bad request + if (read > maxMessageSize) + { + // Message size exceeded + return Results.BadRequest(); + } + + // Attempt to send the buffer to the background queue. + if (queue.Writer.TryWrite(buffer.AsMemory(0..read))) + { + return Results.Accepted(); + } + + // We couldn't accept the message since we're overloaded + return Results.StatusCode(StatusCodes.Status429TooManyRequests); });