C
C#6mo ago
SpReeD

✅ Blazor / ASP.NET - @inject keyed service

Is there a way in .NET 8 to inject a keyed service like a normal service with the @inject keyword? I know of the FromKeyedServices attribute, but cannot figure out how to use it in combination with @inject. I thought of a way like @inject [FromKeyedServices("MyService")] ServiceClass FooBar;
4 Replies
SleepWellPupper
SleepWellPupper6mo ago
Create a property inside a code section or a code-behind file like so:
[Inject]
[FromKeyedServices("MyService"]
public ServiceClass FooBar { get; set; }
[Inject]
[FromKeyedServices("MyService"]
public ServiceClass FooBar { get; set; }
the @inject directive is shorthand for that property without the FromKeyedServicesAttribute
SpReeD
SpReeDOP6mo ago
Idd, so I guess it's not yet implemented, maybe in .NET 9?
SleepWellPupper
SleepWellPupper6mo ago
It is implemented Just not as a razor directive, likely because it is not used often enough to warrant an actual razor-"keyword".
Want results from more Discord servers?
Add your server