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": "*" +} 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 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..a48e03b0ef9f --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultApi/Program.cs @@ -0,0 +1,42 @@ +var builder = WebApplication.CreateBuilder(args); +var app = builder.Build(); + +app.MapGet("/", () => "Hello World!"); +// map the /weatherforecast endpoint to a custom action +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 a typed result + 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..7bc14aa217aa --- /dev/null +++ b/aspnetcore/fundamentals/minimal-apis/misc-samples/typedResults/TypedResultTest/WeatherApiTest.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Http.HttpResults; + +namespace Tests +{ + [TestClass()] + public class WeatherApiTests + { + [TestMethod()] + public void MapWeatherApiTest() + { + var result = WeatherApi.GetAllWeathers(); + // assert that the result is a typed result of type WeatherForecast[] + 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