I created a Web API project alongside a Blazor WebAssembly project in ASP.NET Core (.NET 8). The problem is I want to use ahead-of-time (AOT) compilation for the Blazor WebAssembly project but unfortunately when I publish in release mode, the Blazor WebAssembly application fails to start the platform because it doesn't receive the necessary AOT modules.
All I can say about this error is that I encountered the same issue when I published the Blazor WebAssembly application in release mode (using AOT as well) and tried to run it using IIS Express. When I used dotnet-serve it worked fine. I suspect that all is because some files regarding aot-runtime have to be served for the Blazor WebAssembly application to run properly but that is just speculation on my side.
I'd be happy for someone to help me out with this issue. To make things as clear as possible, I will share some snippets below.
Web API Program.cs
:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddScoped<IPasswordHasher<User>, UserPasswordHasher>();
builder.Services.AddIdentityCore<User>().AddUserStore<UserStore>().AddApiEndpoints();
builder.Services.AddRazorPages();
builder.Services.AddControllers();
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.MapIdentityApi<User>();
app.UseHttpsRedirection();
app.UseBlazorFrameworkFiles();
app.UseStaticFiles();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();
WebAPI.csproj
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Geralt" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10"/>
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WebApp\WebApp.csproj" />
</ItemGroup>
</Project>
WebApp.csproj
:
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<RunAOTCompilation>true</RunAOTCompilation>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.10" PrivateAssets="all" />
</ItemGroup>
</Project>
Edit: I was using a publishing profile, using dotnet publish
solved this issue for me.
You could use the below command to publish the project. make sure you run from the root folder of the solution directory:
dotnet publish -c Release -o ./publish
Here is the official document: