Keyed service hub example (#228)
* Keyed service hub example * Keyed service hub examplepull/231/head
parent
4142b87404
commit
40ee2663f5
|
@ -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,43 @@
|
|||
using Microsoft.AspNetCore.SignalR;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddKeyedSingleton<ICache, BigCache>("big");
|
||||
builder.Services.AddKeyedSingleton<ICache, SmallCache>("small");
|
||||
|
||||
builder.Services.AddRazorPages();
|
||||
builder.Services.AddSignalR();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.MapRazorPages();
|
||||
app.MapHub<MyHub>("/myHub");
|
||||
|
||||
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.";
|
||||
}
|
||||
|
||||
public class MyHub : Hub
|
||||
{
|
||||
public void SmallCacheMethod([FromKeyedServices("small")] ICache cache)
|
||||
{
|
||||
Console.WriteLine(cache.Get("signalr"));
|
||||
}
|
||||
|
||||
public void BigCacheMethod([FromKeyedServices("big")] ICache cache)
|
||||
{
|
||||
Console.WriteLine(cache.Get("signalr"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
Loading…
Reference in New Issue