Skip to content

Commit d026618

Browse files
committed
WIP keyed service from Minimal API
1 parent 6af921d commit d026618

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
var builder = WebApplication.CreateBuilder();
2+
3+
builder.Services.AddKeyedSingleton<IGreeting, MorningGreeting>("morning");
4+
builder.Services.AddKeyedSingleton<IGreeting, DayGreeting>("day");
5+
builder.Services.AddKeyedSingleton<IGreeting, EveningGreeting>("evening");
6+
7+
8+
var app = builder.Build();
9+
10+
app.MapGet("/", (IKeyedServiceProvider provider) =>
11+
{
12+
string GetServiceKey()
13+
{
14+
var currentTime = DateTime.Now;
15+
if (currentTime.Hour < 12)
16+
return "morning";
17+
else if (currentTime.Hour < 18)
18+
return "day";
19+
else
20+
return "evening";
21+
}
22+
23+
var key = GetServiceKey();
24+
var greeting = provider.GetRequiredKeyedService<IGreeting>(key);
25+
26+
return Results.Content("""
27+
<html>
28+
<body>
29+
{{ greeting.Message }}}
30+
</body>
31+
</html>
32+
""", "text/html");
33+
});
34+
35+
app.Run();
36+
37+
38+
interface IGreeting
39+
{
40+
string Message { get; }
41+
}
42+
43+
public class MorningGreeting : IGreeting
44+
{
45+
public string Message => "Good morning";
46+
}
47+
48+
public class DayGreeting : IGreeting
49+
{
50+
public string Message => "Good day";
51+
}
52+
53+
public class EveningGreeting : IGreeting
54+
{
55+
public string Message => "Good evening";
56+
}
57+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using Keyed Service with Minimal API
2+
3+
Use `IKeyedServiceProvider.GetRequiredKeyedService<T>(key)` to get different type of implementation instances based on string key.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<ImplicitUsings>true</ImplicitUsings>
5+
<LangVersion>preview</LangVersion>
6+
<PreviewFeatures>true</PreviewFeatures>
7+
</PropertyGroup>
8+
</Project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.5.002.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "keyed-service", "keyed-service.csproj", "{1A4EF9A9-14AE-464D-8431-B35DA014E9D7}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{1A4EF9A9-14AE-464D-8431-B35DA014E9D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1A4EF9A9-14AE-464D-8431-B35DA014E9D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1A4EF9A9-14AE-464D-8431-B35DA014E9D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1A4EF9A9-14AE-464D-8431-B35DA014E9D7}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {2D48A138-5215-428D-8CF4-56F21D0D4194}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)