Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

BackgroundService event to trigger UI update in Blazor Server

$
0
0

Question Up Front: I am looking for a way to trigger a UI update from a BackgroundService in a Blazor Server app.

I have a Blazor Server app that displays data from a DB. The data in the DB is sourced from an external API and transformed/calculated prior to storing/updating. As this is an ongoing process and user agnostic, I have implemented this using a BackgroundService (First time using background services).

public class DataManagementService(IServiceScopeFactory scopeFactory, IApiService apiService, IEncryptionService encryptionService) : BackgroundService {    private readonly IApiService _apiService = apiService;    private readonly IEncryptionService _encryptionService = encryptionService;    protected override async Task ExecuteAsync(CancellationToken stoppingToken)    {        Log.Information("================= DataManagementService Starting =================");        Log.Information("================= Connecting to API =================");        await ConnectToApi();        while (!stoppingToken.IsCancellationRequested)        {            Log.Information("================= Data Update Starting =================");            await UpdateData();            Log.Information("================= Data Update Completed =================");            await Task.Delay(60000, stoppingToken);        }    }}

The BackgroundService is functioning correctly in relation to updating the data. During some iterations of the data updating process, there may be information that needs to be updated within the UI. Is there a way to trigger a UI update from the BackgroundService? My initial thought was that I may be able to use events to achieve this and have the UI subscribed to those events, but I have not been able to find any information that supports this theory.

The way I currently have this working is by using a crude implementation of running a UI update every minute; however, I am looking for a more efficient, reactive (no time lag between update event and display) and arguably "proper" way.

Grateful for any assistance or guidance you can provide.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>