I am new to Blazor and started with a YouTube tutorial.
Therefore I created a Blazor Web App project in VS 2022 (latest version) and .NET Runtime 8.0.6. I included the sample code (Weather App) in the project and let it run without changing anything of the sample code.
As i open the Weather Tab, it is supposed to instantly open the weather page, display “Loading…” for 500ms and show a table with some temperatures afterwards. In my case it waits the 500ms before opening the weater tab and instantly shows the table.
Here is the code of the blazor sample file:
@page "/weather"
@attribute [StreamRendering]
<PageTitle>Weather</PageTitle>
<h1>Weather</h1>
<p>This component demonstrates showing data.</p>
@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}
@code {
private WeatherForecast[]? forecasts;
protected override async Task OnInitializedAsync()
{
// Simulate asynchronous loading to demonstrate streaming rendering
await Task.Delay(2000);
var startDate = DateOnly.FromDateTime(DateTime.Now);
var summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" };
forecasts = Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = summaries[Random.Shared.Next(summaries.Length)]
}).ToArray();
}
private class WeatherForecast
{
public DateOnly Date { get; set; }
public int TemperatureC { get; set; }
public string? Summary { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
}
Therefore the @attribute [StreamRendering] is not working because it does not run the OnInitializedAsync() method asynchronously on the server side.
There is also a GitHub post showing the exact same issue https://github.com/dotnet/aspnetcore/issues/52323.
This one is more related to the issue of IIS compressing and does not contain a solution for me. In my case it does not work with http, https or IIS either.
It also does not matter which Browser I use and it is also not working on my colleagues PC.
I am already frustrated before I even started with Blazor…