I want to use a scoped service in a method of a singleton service in server-side Blazor.
I found out that you can use the [FromServices] Attribute in the function parameters, but when I call the method I get a compiler error for calling the method with less parameters.
I tried calling the method with a dummy parameter in the hope it would get overwritten when called, but that was not the case.
Scoped Service:
public class UserContext
{
public string SomeUserRelatedData {get; set;}
}
Singleton service:
public class GlobalService
{
...
public void DoSomethingWithUserData([FromServices]UserContext usercontext)
{
...
}
}
Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSingleton<GlobalService>();
services.AddScoped<UserContext>();
}
}