From da46e1d233fb125183622f28b4321ece1f18466f Mon Sep 17 00:00:00 2001 From: Rick Anderson <3605364+Rick-Anderson@users.noreply.github.com> Date: Thu, 12 Oct 2023 12:48:44 -1000 Subject: [PATCH] KeySvc --- .../KeyServiceController.csproj | 9 ++++ .../samples/KeyServiceController/Program.cs | 49 +++++++++++++++++++ .../KeyServiceController/appsettings.json | 9 ++++ .../KeyServiceMinAPI/KeyServiceMinAPI.csproj | 9 ++++ .../samples/KeyServiceMinAPI/Program.cs | 26 ++++++++++ .../samples/KeyServiceMinAPI/appsettings.json | 9 ++++ 6 files changed, 111 insertions(+) create mode 100644 fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj create mode 100644 fundamentals/minimal-apis/samples/KeyServiceController/Program.cs create mode 100644 fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json create mode 100644 fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj create mode 100644 fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs create mode 100644 fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj b/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj new file mode 100644 index 0000000..1b28a01 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/KeyServiceController.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs b/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs new file mode 100644 index 0000000..3157a40 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/Program.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddKeyedSingleton("big"); +builder.Services.AddKeyedSingleton("small"); +builder.Services.AddControllers(); + +var app = builder.Build(); + +app.MapControllers(); + +app.Run(); + +public interface ICache +{ + object Get(string key); +} +public class BigCache : ICache +{ + public object Get(string key) => $"Resolving {key} from big cache."; +} + +public class SmallCache : ICache +{ + public object Get(string key) => $"Resolving {key} from small cache."; +} + +[ApiController] +[Route("/cache")] +public class CustomServicesApiController : Controller +{ + public ActionResult GetOk(ICache cache) + { + return cache.Get("data-mvc"); + } + + [HttpGet("big")] + public ActionResult GetBigCache([FromKeyedServices("big")] ICache cache) + { + return GetOk(cache); + } + + [HttpGet("small")] + public ActionResult GetSmallCache([FromKeyedServices("small")] ICache cache) + { + return GetOk(cache); + } +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json b/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceController/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj new file mode 100644 index 0000000..1b28a01 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/KeyServiceMinAPI.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs new file mode 100644 index 0000000..e8adc48 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/Program.cs @@ -0,0 +1,26 @@ +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddKeyedSingleton("big"); +builder.Services.AddKeyedSingleton("small"); + +var app = builder.Build(); + +app.MapGet("/big", ([FromKeyedServices("big")] ICache bigCache) => bigCache.Get("date")); + +app.MapGet("/small", ([FromKeyedServices("small")] ICache smallCache) => smallCache.Get("date")); + +app.Run(); + +public interface ICache +{ + object Get(string key); +} +public class BigCache : ICache +{ + public object Get(string key) => $"Resolving {key} from big cache."; +} + +public class SmallCache : ICache +{ + public object Get(string key) => $"Resolving {key} from small cache."; +} diff --git a/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/fundamentals/minimal-apis/samples/KeyServiceMinAPI/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +}