To ensure your Blazor WebAssembly components work correctly during the pre-rendering process, you must extract your service registration logic into a static local function named static void ConfigureServices(IServiceCollection services, string baseAddress) within your Program.cs.
The package looks for this specific function signature to initialize services during pre-rendering.
Supported Signatures:
static void ConfigureServices(IServiceCollection services, string baseAddress): The string argument is treated as the app's base address.static void ConfigureServices(IServiceCollection services, IConfiguration configuration): Uses the contents of wwwroot/appsetting.json.static void ConfigureServices(IServiceCollection services, IWebAssemblyHostEnvironment webHostEnv, IConfiguration configuration): Use this if you need to access the environment name via webHostEnv.Environment or the base address via webHostEnv.BaseAddress.
// Program.cs
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");
ConfigureServices(builder.Services, builder.HostEnvironment.BaseAddress);
await builder.Build().RunAsync();
static void ConfigureServices(IServiceCollection services, string baseAddress)
{
services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(baseAddress) });
services.AddScoped<IFoo, MyFoo>();
}