Application Configuration
Overview
This document covers the core configuration setup of the APEX Portal application, focusing on the Program.cs file which serves as the main entry point and configuration hub for the Blazor WebAssembly application.
Prerequisites
- .NET 8.0 SDK or later
- Blazor WebAssembly project structure
- Knowledge of dependency injection concepts
Configuration Structure
The Program.cs file is responsible for:
- Initializing the WebAssembly host builder
- Registering root components
- Configuring HTTP clients
- Setting up dependency injection for services
- Building and running the application
API Reference
Root Components Registration
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
HTTP Client Configuration
Base HTTP Client
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(new Uri(builder.HostEnvironment.BaseAddress), "./api/") });
Specialized HTTP Clients
builder.Services.AddHttpClient<BillRunsTimelineEventDataProvider>(
client => {
client.BaseAddress = new Uri(new Uri(builder.HostEnvironment.BaseAddress), "./api/billing/");
});
Framework Services
builder.Services.AddBlazorBootstrap();
Application Services
builder.Services.AddTransient<ITimelineEventDataProvider, BillRunsTimelineEventDataProvider>();
Examples
Adding a New API Client
To add a new API client for a different module:
builder.Services.AddHttpClient<ProductsDataProvider>(
client => {
client.BaseAddress = new Uri(new Uri(builder.HostEnvironment.BaseAddress), "./api/products/");
});
builder.Services.AddTransient<IProductsDataProvider, ProductsDataProvider>();
Adding Authentication
To add authentication services:
builder.Services.AddMsalAuthentication(options =>
{
builder.Configuration.Bind("AzureAd", options.ProviderOptions.Authentication);
options.ProviderOptions.DefaultAccessTokenScopes.Add("api://scope");
});
Best Practices
- Keep service registrations organized by function or module
- Use typed HTTP clients for better maintainability
- Configure appropriate base addresses for API endpoints
- Use transient lifetime for data providers
- Keep the Program.cs file focused only on configuration
Common Issues and Solutions
Issue: API calls resulting in 404 errors
Solution: Verify that the base address configuration correctly points to your API endpoints.
Issue: Services not available for injection
Solution: Ensure that services are registered with the appropriate lifetime (Singleton, Scoped, or Transient).
Issue: Configuration not loading from appsettings.json
Solution: Verify that appsettings.json is properly included in the project and marked as "Content" in the build properties.
Was this page useful?