Skip to content

Commit bcbfe33

Browse files
committed
WIP two samples about getting keyed service. Still waiting for RC1 to work properly.
1 parent d026618 commit bcbfe33

File tree

6 files changed

+110
-2
lines changed

6 files changed

+110
-2
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
var builder = WebApplication.CreateBuilder();
4+
5+
builder.Services.AddKeyedSingleton<IGreeting, MorningGreeting>("morning");
6+
builder.Services.AddKeyedSingleton<IGreeting, DayGreeting>("day");
7+
builder.Services.AddKeyedSingleton<IGreeting, EveningGreeting>("evening");
8+
builder.Services.AddControllers();
9+
10+
var app = builder.Build();
11+
app.MapControllers();
12+
13+
app.Run();
14+
15+
public class HomeController: ControllerBase
16+
{
17+
18+
readonly IServiceProvider _keyProvider;
19+
20+
public HomeController(IServiceProvider keyProvider)
21+
{
22+
_keyProvider = keyProvider;
23+
}
24+
25+
[HttpGet("/")]
26+
public IActionResult Index()
27+
{
28+
string GetServiceKey()
29+
{
30+
var currentTime = DateTime.Now;
31+
if (currentTime.Hour < 12)
32+
return "morning";
33+
else if (currentTime.Hour < 18)
34+
return "day";
35+
else
36+
return "evening";
37+
}
38+
39+
var key = GetServiceKey();
40+
var greeting = _keyProvider.GetRequiredKeyedService<IGreeting>(key);
41+
42+
return Content("""
43+
<html>
44+
<body>
45+
{{ greeting.Message }}}
46+
</body>
47+
</html>
48+
""", "text/html");
49+
}
50+
}
51+
52+
53+
interface IGreeting
54+
{
55+
string Message { get; }
56+
}
57+
58+
public class MorningGreeting : IGreeting
59+
{
60+
public string Message => "Good morning";
61+
}
62+
63+
public class DayGreeting : IGreeting
64+
{
65+
public string Message => "Good day";
66+
}
67+
68+
public class EveningGreeting : IGreeting
69+
{
70+
public string Message => "Good evening";
71+
}
72+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Using Keyed Service with MVC
2+
3+
Use `IServiceProvider.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-2", "keyed-service-2.csproj", "{998CB752-9F46-470D-A2C6-669C3D23897F}"
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+
{998CB752-9F46-470D-A2C6-669C3D23897F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{998CB752-9F46-470D-A2C6-669C3D23897F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{998CB752-9F46-470D-A2C6-669C3D23897F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{998CB752-9F46-470D-A2C6-669C3D23897F}.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 = {F6B469E4-30D1-4F0D-896D-139274C08BE2}
24+
EndGlobalSection
25+
EndGlobal

projects/.net8/keyed-service/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
var app = builder.Build();
99

10-
app.MapGet("/", (IKeyedServiceProvider provider) =>
10+
app.MapGet("/", (IServiceProvider provider) =>
1111
{
1212
string GetServiceKey()
1313
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Using Keyed Service with Minimal API
22

3-
Use `IKeyedServiceProvider.GetRequiredKeyedService<T>(key)` to get different type of implementation instances based on string key.
3+
Use `IServiceProvider.GetRequiredKeyedService<T>(key)` to get different type of implementation instances based on string key.

0 commit comments

Comments
 (0)