Access a service through code (not a cshtml file)
Is there currently anyway to get a reference to a service (particularly, a singleton) through a client class other than a .cshtml?
I tried this:
public class TestUtils : Microsoft.AspNetCore.Blazor.Components.BlazorComponent
{
[Microsoft.AspNetCore.Blazor.Components.Inject]
public Services.AppState appstate { get; set; }
}
But my appstate property is always null.
If there is not currently a way to do this I propose that it be added.
/joel
This is really a question about ASP.NET Core's DI mechanism in general, not Blazor in particular. Your question would be the same for a traditional server-side ASP.NET Core application.
AFAIK there isn't a static, context-free way of accessing DI services in ASP.NET because you can have any number of service providers. How would it know which one should supply the service? You have to have a reference to a particular service provider and then call the GetRequiredService<T> method on it. There's more documentation about DI in ASP.NET here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.1