✅ 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
Create a property inside a
code
section or a code-behind file like so:
the @inject
directive is shorthand for that property without the FromKeyedServicesAttribute
ASP.NET Core Blazor dependency injection
Learn how Blazor apps can inject services into components.
Idd, so I guess it's not yet implemented, maybe in .NET 9?
It is implemented
Just not as a razor directive, likely because it is not used often enough to warrant an actual razor-"keyword".