A simple test for maybe doing events in the backgrounds between pages in Blazor.
at main 1.5 kB view raw
1using EventsInBlazorTests.Components; 2using EventsInBlazorTests.Services; 3using Microsoft.AspNetCore.Mvc; 4 5var builder = WebApplication.CreateBuilder(args); 6 7// Add services to the container. 8builder.Services.AddRazorComponents() 9 .AddInteractiveServerComponents(); 10 11builder.Services.AddSingleton<NotifyService>(); 12 13var app = builder.Build(); 14 15// Configure the HTTP request pipeline. 16if (!app.Environment.IsDevelopment()) 17{ 18 app.UseExceptionHandler("/Error", createScopeForErrors: true); 19 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. 20 app.UseHsts(); 21} 22 23app.UseHttpsRedirection(); 24 25 26app.UseAntiforgery(); 27 28app.MapStaticAssets(); 29app.MapRazorComponents<App>() 30 .AddInteractiveServerRenderMode(); 31 32// API-Endpunkt zum Auslösen von Benachrichtigungen 33app.MapGet("/api/notify", ([FromServices] ILogger<Program> logger, [FromServices] NotifyService notifyService) => 34{ 35 logger.LogInformation("Anfrage zum Auslösen eines Ereignisses erhalten"); 36 try 37 { 38 // Sendet eine Notification an den NotifyService, welches die Informationen an die Komponenten weitergibt 39 notifyService.SendNotification(); 40 } 41 catch (Exception ex) 42 { 43 logger.LogError(ex, "Fehler beim Auslösen des Ereignisses"); 44 return Results.Problem(ex.Message); 45 } 46 logger.LogInformation("Ereignis erfolgreich ausgelöst"); 47 return Results.Ok(); 48}); 49 50app.Run();