Closed nssidhu closed 4 years ago
Can you share a minimal app that reproduces the issue? We suspect the app must be making a blocking call (Task.Result
) at some point to encounter this error.
@page "/"
<h1>Hello, world!</h1>
<table>
<thead>
<tr>
<th>Number</th>
<th>Multiple</th>
<th>Total</th>
</tr>
</thead>
@foreach(KeyValuePair<int, int> num in Nums)
{
<tr>
<td>@num.Key</td>
<td>@num.Value</td>
<td>@GetMultiple(num.Key,num.Value).Result</td>
</tr>
}
</table>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<input type="button" @onclick="LoadData" value="LaodData"/>
@code{
Dictionary<int, int> Nums = new Dictionary<int, int>();
async Task LoadData()
{
Nums.Clear();
Nums.Add(7, 2);
Nums.Add(8, 2);
Nums.Add(3, 2);
Nums.Add(9, 2);
Nums.Add(4, 2);
StateHasChanged();
}
async Task<int> GetMultiple(int number, int multiplier)
{
try{
await Task.Delay(20000);
return number * multiplier;
}
catch(Exception e)
{
return - 1;
}
}
}
I suspect this is exactly what @pranavkm meant:
<td>@GetMultiple(num.Key,num.Value).Result</td>
You are using .Result
here, which is attempting to block the thread. This will not work on Blazor. Instead, try fetching the data into a variable that you display, and once the result arrives, call StateHasChanged()
, which will update the view.
Thanks @nikonthethird.
As an additional note, you'll want to do this in a lifecycle method like OnInitializedAsync
. You don't need to call StateHasChanged
since Blazor will take care of that for you.
I am making a call to GoogleMap GeoCode to get back the lat & Long for the given address.
var result = await JSRuntime.InvokeAsync("getIsInRange", address, range);
Console.WriteLine("Recieved data " + result);
I am calling this from within loop for multiple address to be displayed in the table.
if i remove await the error goes away but than i don't get the lat & long since that it self is asyn on javascript side. What are my options here ?