From 051f7377e7490198815454437f1c7516f269a44c Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Tue, 22 Mar 2022 17:16:11 +0100 Subject: [PATCH 1/9] removed problematic condition --- .../index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs index 2d22ffc4f5ce..c870ef4b0ef6 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs @@ -18,12 +18,7 @@ public ArrayModel(IConfiguration config) public ContentResult OnGet() { - if (_array == null) - { - throw new ArgumentNullException(nameof(_array)); - } - - _array = Config.GetSection("array").Get(); + _array = Config.GetSection("array").Get(); string s = String.Empty; for (int j = 0; j < _array.Entries.Length; j++) @@ -35,4 +30,4 @@ public ContentResult OnGet() } } #endregion -} \ No newline at end of file +} From f0fae69985d3c51b4fd4953b298f238526c332f5 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Tue, 22 Mar 2022 17:17:15 +0100 Subject: [PATCH 2/9] removed problematic condition --- .../index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs index c870ef4b0ef6..656cddc073ba 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs @@ -18,7 +18,7 @@ public ArrayModel(IConfiguration config) public ContentResult OnGet() { - _array = Config.GetSection("array").Get(); + _array = Config.GetSection("array").Get(); string s = String.Empty; for (int j = 0; j < _array.Entries.Length; j++) From 1a43a00698250927d1ed71fc18c6a65d560221c6 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Tue, 22 Mar 2022 17:24:47 +0100 Subject: [PATCH 3/9] changed condition position 'the conditional statement should come after ``` _array = _c.GetSection("array").Get(); ``` --- .../index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs index 656cddc073ba..d14533e89ed2 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs @@ -18,7 +18,11 @@ public ArrayModel(IConfiguration config) public ContentResult OnGet() { - _array = Config.GetSection("array").Get(); + _array = _c.GetSection("array").Get(); + if (_array == null) + { + throw new ArgumentNullException(nameof(_array)); + } string s = String.Empty; for (int j = 0; j < _array.Entries.Length; j++) From 2c1664c5e145f1854ad82898416e990fbd529e20 Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Tue, 22 Mar 2022 17:33:43 +0100 Subject: [PATCH 4/9] removed early initialization The removal of the early initialization avoids the problem of having empty indexes when the controller processes the request --- .../index/samples/6.x/ConfigSample/Options/ArrayExample.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Options/ArrayExample.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Options/ArrayExample.cs index a49d6d727ce0..5d3d2074df62 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Options/ArrayExample.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Options/ArrayExample.cs @@ -3,7 +3,7 @@ #region snippet public class ArrayExample { - public string[] Entries { get; set; } = new string[] { "", "" }; + public string[]? Entries { get; set; } } #endregion } From 217d98fb47c56a9be20968c6f0ab96badbae2e8e Mon Sep 17 00:00:00 2001 From: sammychinedu2ky Date: Wed, 23 Mar 2022 03:38:03 +0100 Subject: [PATCH 5/9] changed condition position --- .../index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs index d14533e89ed2..3a7fd904c2e8 100644 --- a/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs +++ b/aspnetcore/fundamentals/configuration/index/samples/6.x/ConfigSample/Pages/Array.cshtml.cs @@ -18,7 +18,7 @@ public ArrayModel(IConfiguration config) public ContentResult OnGet() { - _array = _c.GetSection("array").Get(); + _array = Config.GetSection("array").Get(); if (_array == null) { throw new ArgumentNullException(nameof(_array)); From 744f14eafa63d60eea4a3d4fbb6aa65bf82c07a4 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 9 Jul 2022 12:41:04 +0100 Subject: [PATCH 6/9] deleted readme.txt --- .../minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/readme.txt 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 8dffbb7fd27fa3f10b1dc01fdf3ea181c3461c80 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Sat, 9 Jul 2022 13:49:48 +0100 Subject: [PATCH 7/9] created sample code --- .../BackgroundQueueService.cs | 47 ++++++++++++++++++ .../PipeStreamToBackgroundQueue.csproj | 9 ++++ .../PipeStreamToBackgroundQueue/Program.cs | 48 +++++++++++++++++++ .../appsettings.Development.json | 8 ++++ .../appsettings.json | 9 ++++ 5 files changed, 121 insertions(+) create mode 100644 aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs 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/BackgroundQueueService.cs b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/BackgroundQueueService.cs new file mode 100644 index 000000000000..8af612df9729 --- /dev/null +++ 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 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..2adf4354316d --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/bindStreamPipeReader/7.0-samples/PipeStreamToBackgroundQueue/Program.cs @@ -0,0 +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(); 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 01c3440ff745f46c8917cf437b45c7df11fbb531 Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Tue, 12 Jul 2022 10:09:25 +0100 Subject: [PATCH 8/9] sample code for typed result --- .../typedResults/TypedResultApi/Program.cs | 41 +++++++++++++++++++ .../TypedResultApi/TypedResultApi.csproj | 9 ++++ .../appsettings.Development.json | 8 ++++ .../TypedResultApi/appsettings.json | 9 ++++ .../TypedResultTest/TypedResultTest.csproj | 21 ++++++++++ .../typedResults/TypedResultTest/Usings.cs | 1 + .../TypedResultTest/WeatherApiTest.cs | 20 +++++++++ .../misc-samples/typedResults/readme.txt | 1 - 8 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/TypedResultApi.csproj create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.Development.json create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.json create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/TypedResultTest.csproj create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/Usings.cs create mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs delete mode 100644 aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs new file mode 100644 index 000000000000..8fd955eb3fdb --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs @@ -0,0 +1,41 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); + +app.MapWeatherApi(); +app.Run(); + + + +public static class WeatherApi +{ + public static string[] summaries = +{ + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" +}; + + public static WebApplication MapWeatherApi(this WebApplication routes) + { + routes.MapGet("/weatherforecast", GetAllWeathers); + + return routes; + } + public static IResult GetAllWeathers() + { + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateTime.Now.AddDays(index), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return TypedResults.Ok(forecast); ; + } +} + +public record WeatherForecast(DateTime Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} \ No newline at end of file diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/TypedResultApi.csproj b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/TypedResultApi.csproj new file mode 100644 index 000000000000..4c2bb77d0106 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/TypedResultApi.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.Development.json b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.Development.json new file mode 100644 index 000000000000..0c208ae9181e --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.json b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.json new file mode 100644 index 000000000000..10f68b8c8b4f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/TypedResultTest.csproj b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/TypedResultTest.csproj new file mode 100644 index 000000000000..24092ef854ba --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/TypedResultTest.csproj @@ -0,0 +1,21 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + + + + + + diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/Usings.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/Usings.cs new file mode 100644 index 000000000000..540383dcf43c --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs new file mode 100644 index 000000000000..3ceb176f19f4 --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs @@ -0,0 +1,20 @@ +using Microsoft.AspNetCore.Http.HttpResults; + +namespace Tests +{ + [TestClass()] + public class WeatherApiTests + { + [TestMethod()] + public void MapWeatherApiTest() + { + //act + var result = WeatherApi.GetAllWeathers(); + Console.WriteLine("hi"); + + Assert.IsInstanceOfType(result, typeof(Ok)); + } + + } + +} \ No newline at end of file diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt deleted file mode 100644 index 655bdceab454..000000000000 --- a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/readme.txt +++ /dev/null @@ -1 +0,0 @@ -Create sample code here then delete this file. \ No newline at end of file From 20c7f119d6752230acddf2b7e1ac7443c53d6c6e Mon Sep 17 00:00:00 2001 From: Samson Amaugo Date: Tue, 12 Jul 2022 10:24:00 +0100 Subject: [PATCH 9/9] added some comments --- .../misc-samples/typedResults/TypedResultApi/Program.cs | 3 ++- .../typedResults/TypedResultTest/WeatherApiTest.cs | 4 +--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs index 8fd955eb3fdb..a48e03b0ef9f 100644 --- a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs @@ -2,7 +2,7 @@ var app = builder.Build(); app.MapGet("/", () => "Hello World!"); - +// map the /weatherforecast endpoint to a custom action app.MapWeatherApi(); app.Run(); @@ -31,6 +31,7 @@ public static IResult GetAllWeathers() summaries[Random.Shared.Next(summaries.Length)] )) .ToArray(); + // return a typed result return TypedResults.Ok(forecast); ; } } diff --git a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs index 3ceb176f19f4..7bc14aa217aa 100644 --- a/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs @@ -8,10 +8,8 @@ public class WeatherApiTests [TestMethod()] public void MapWeatherApiTest() { - //act var result = WeatherApi.GetAllWeathers(); - Console.WriteLine("hi"); - + // assert that the result is a typed result of type WeatherForecast[] Assert.IsInstanceOfType(result, typeof(Ok)); }