Injecting in to class inheriting from RevalidatingServerAuthenticationStateProvider

735 Views Asked by At

I am using the RevalidatingServerAuthenticationStateProvider to tirgger a refresh of an access token in blazor.

I would like to inject the IDispatcher from Fluxor, in order to dispatch new tokens once the new access code has been generated.

I cannot inject IDispatcher in to the class

If I inject in to the constructor, blazor starts with a blank screen and no error is reported.

Using the [Inject] attribute just results in a null property.

Note: I can inject IDispatcher elsewhere and is used, and it is registered in DI.

Any advice on how to dispatch a message from inside?

Im using blazor server if that makes any difference.

1

There are 1 best solutions below

2
On

My guess is that you are not registering your class with Dependency Injection container.

Example of Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IMyClass, MyClass>();
}

Then you can use like so
razor

@inject IMyClass MyClassVariable

c#

[Inject]
protected IMyClass MyClassVariable { get; set; }

If you need to do it in the component's scope, you can do something like this

protected override void OnInitialized()
{
    MyClassService = ScopedServices.GetRequiredService<IMyClass>();
}

Check out Microsoft docs for more info