// Program.cs -- Blazor WebAssembly application entry point for ChatAgent. // // This is where the WASM client is configured and launched. Unlike a server-side // ASP.NET Core app, a Blazor WASM app runs entirely in the browser. The // WebAssemblyHostBuilder configures: // - Root components (what gets rendered into the HTML page) // - Services (dependency injection container, similar to server-side DI) // - Configuration (reads from wwwroot/appsettings.json) // // This file will grow as we add more services in later phases (e.g., chat state management). using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using ChatAgent.Client; using ChatAgent.Client.Services; var builder = WebAssemblyHostBuilder.CreateDefault(args); // Add("#app") tells Blazor to render the App component inside the //
element in wwwroot/index.html. This is the "mount point" // for the entire Blazor component tree. builder.RootComponents.Add("#app"); // HeadOutlet allows Razor components to modify elements (e.g., ) // using the <PageTitle> and <HeadContent> components. "head::after" means // content is appended after existing head elements. builder.RootComponents.Add<HeadOutlet>("head::after"); // Read the API base URL from configuration. // In Blazor WASM, configuration comes from wwwroot/appsettings.json. // IMPORTANT: wwwroot/ files are PUBLIC -- they are downloaded to the browser. // Never put secrets (API keys, passwords) in appsettings.json for a WASM app. // The API key lives server-side in the ChatAgent.Api project. var apiBaseUrl = builder.Configuration["ApiBaseUrl"] ?? "https://localhost:7100"; // AddHttpClient<ChatApiClient> registers a typed HttpClient using IHttpClientFactory. // IHttpClientFactory manages the underlying HttpMessageHandler lifetime to prevent // socket exhaustion (a common problem with raw HttpClient in long-running apps). // The lambda configures the client with the API base URL so ChatApiClient // does not need to know the URL -- it is injected with a pre-configured HttpClient. // In Blazor WASM, HttpClient uses the browser's Fetch API under the hood. builder.Services.AddHttpClient<ChatApiClient>(client => { client.BaseAddress = new Uri(apiBaseUrl); }); await builder.Build().RunAsync();