Closed JDBarndt closed 4 years ago
Does the call to GetForecastAsync
largely perform work synchronously? The pattern specifically works if work's being done asynchronously. That might explain why the Task.Delay
has a visible outcome.
Ah, looks like you're right! I didn't realize the GetForecastAsync
function is in fact not asynchronous:
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
var rng = new Random();
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToArray());
}
Refactoring to the below seems to have done the trick:
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
return Task.Run(() =>
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
}).ToArray();
});
}
Looks like the default Blazor project template could use an update so those of us following the documentation don't run into this. 👍
Thanks!
Describe the bug
Stumbled into this while following documentation here: https://docs.microsoft.com/en-us/aspnet/core/security/blazor/server?view=aspnetcore-3.0#guard-against-multiple-dispatches
While following the guard against multiple dispatches example in the URL above, the button is not disabled (visibly) to end-user while the forecast data is being retrieved.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
The button should be disabled while isLoading = true.
Additional context
This is my full source of FetchData.razor:
You'll notice I left a commented line in there,
await Task.Delay(1);
. That is the workaround I have found in order to get the button to show as disabled while updating the forecast.