Added server-side DI registrations for authentication and app-specific services to support Blazor InteractiveAuto prerendering. Registered a scoped HttpClient with base URI for correct API routing. Switched ApiBaseUrl in appsettings.json to https://localhost:8088. Added necessary using statements for new services and providers.
72 lines
2.8 KiB
C#
72 lines
2.8 KiB
C#
using EnvelopeGenerator.ReceiverUI.Client.Auth;
|
|
using EnvelopeGenerator.ReceiverUI.Client.Services;
|
|
using EnvelopeGenerator.ReceiverUI.Client.State;
|
|
using EnvelopeGenerator.ReceiverUI.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents()
|
|
.AddInteractiveWebAssemblyComponents();
|
|
|
|
// API-Proxy: Alle /api/* Aufrufe an die echte API weiterleiten
|
|
// WARUM: Der Blazor-Client ruft /api/envelope auf. Diese Anfrage geht an den
|
|
// ReceiverUI-Server (gleiche Domain, kein CORS), der sie an die echte API weiterleitet.
|
|
var apiBaseUrl = builder.Configuration["ApiBaseUrl"]
|
|
?? throw new InvalidOperationException("ApiBaseUrl is not configured in appsettings.json.");
|
|
|
|
builder.Services.AddHttpForwarder();
|
|
|
|
// ── Services: Müssen AUCH auf dem Server registriert sein ──
|
|
// WARUM? Bei @rendermode InteractiveAuto rendert Blazor die Seite zuerst
|
|
// auf dem Server (SSR/Prerendering). Dabei resolved es @inject-Properties.
|
|
// Wenn ein Service nur im Client-Projekt (WASM) registriert ist, aber nicht
|
|
// hier, gibt es eine InvalidOperationException beim Prerendering.
|
|
//
|
|
// Der HttpClient auf dem Server zeigt auf sich selbst (localhost),
|
|
// weil die /api/* Requests über MapForwarder an die echte API gehen.
|
|
builder.Services.AddScoped(sp =>
|
|
{
|
|
var navigationManager = sp.GetService<Microsoft.AspNetCore.Components.NavigationManager>();
|
|
var baseUri = navigationManager?.BaseUri ?? $"https://localhost:{builder.Configuration["ASPNETCORE_HTTPS_PORT"] ?? "7206"}/";
|
|
return new HttpClient { BaseAddress = new Uri(baseUri) };
|
|
});
|
|
|
|
builder.Services.AddAuthorizationCore();
|
|
builder.Services.AddScoped<ApiAuthStateProvider>();
|
|
builder.Services.AddScoped<AuthenticationStateProvider>(sp =>
|
|
sp.GetRequiredService<ApiAuthStateProvider>());
|
|
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddScoped<IEnvelopeService, EnvelopeService>();
|
|
builder.Services.AddScoped<IReceiverAuthService, ReceiverAuthService>();
|
|
builder.Services.AddScoped<EnvelopeState>();
|
|
builder.Services.AddScoped<ToastService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseWebAssemblyDebugging();
|
|
}
|
|
else
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseStaticFiles();
|
|
app.UseAntiforgery();
|
|
|
|
// Alle /api/* Requests an die echte EnvelopeGenerator.API weiterleiten
|
|
// So muss der Browser nie direkt mit der API sprechen → kein CORS, Cookies funktionieren
|
|
app.MapForwarder("/api/{**catch-all}", apiBaseUrl);
|
|
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode()
|
|
.AddInteractiveWebAssemblyRenderMode()
|
|
.AddAdditionalAssemblies(typeof(EnvelopeGenerator.ReceiverUI.Client._Imports).Assembly);
|
|
|
|
app.Run(); |