KeySvc
parent
bd331c43d4
commit
da46e1d233
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,49 @@
|
|||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddKeyedSingleton<ICache, BigCache>("big");
|
||||
builder.Services.AddKeyedSingleton<ICache, SmallCache>("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<object> GetOk(ICache cache)
|
||||
{
|
||||
return cache.Get("data-mvc");
|
||||
}
|
||||
|
||||
[HttpGet("big")]
|
||||
public ActionResult<object> GetBigCache([FromKeyedServices("big")] ICache cache)
|
||||
{
|
||||
return GetOk(cache);
|
||||
}
|
||||
|
||||
[HttpGet("small")]
|
||||
public ActionResult<object> GetSmallCache([FromKeyedServices("small")] ICache cache)
|
||||
{
|
||||
return GetOk(cache);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
|
@ -0,0 +1,26 @@
|
|||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddKeyedSingleton<ICache, BigCache>("big");
|
||||
builder.Services.AddKeyedSingleton<ICache, SmallCache>("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.";
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Loading…
Reference in New Issue