I have been messing around with the HttpClient so far and wanted to create an async call to an Endpoint via a button click.
Something like this.
// Something that should update once the async call is done
if(!isDone) { <text>Processing</text> }
else { <text>Done: @responseText</text> }
// Button
<button type="button" @onclick(GetClick)>Get Some Data</button>
// Function
async Task GetClick()
{
responseText = await Http.GetStringAsync("/");
isDone = true;
}
The problem is that onClick directive doesn't allow a function that returns anything it seems and only accepts void. By changing the return type to void from Task, it works but since it's an async task the Page doesn't update it's view when the async task is done and isDone is set to true since I think the component isn't notified.
The workaround I did is to put StateHasChanged() as the last call on the async void function to manually notify the component to render.
Maybe i'm doing something wrong and there's already a way for this though. Just to clarify I guess.
According to the commit and the namespace decl, this is just a temporary HTTP Client handler because there isn鈥檛 an implementation of the client from mono. The current implementation is going to change soon.
Maybe i'm doing something wrong and there's already a way for this though. Just to clarify I guess.
No, you are correct. We just haven't implemented onclickAsync yet. In the meantime you need to call StateHasChanged manually.
Ah good to know, thanks for the clarification.
Maybe i'm doing something wrong and there's already a way for this though. Just to clarify I guess.
No, you are correct. We just haven't implemented
onclickAsyncyet. In the meantime you need to callStateHasChangedmanually.
DOTNET SDK : 2.1.403
OS : Mac Mojave
Browser : Safari 12.0 (14606.1.36.1.9)
Blazor : Serverside - 0.6.0
I've recently started using Async Void to allow the response of an http call to update the UI. In order to get it working I manually call StateHasChanged.
Every once in a while the UI update won't fire which I think is something to do with latency (no proof) but what's interesting is eventually the dotnet process will rise and pin the CPU.
I've checked for exceptions, nothing is thrown.
Currently I don't have a workaround for this.
Any ideas?
Binding :
<span onclick="@(() => SwitchEnvironment(availableEnvironments[loopId]))"><span>
Function :
private async void SwitchEnvironment(string id)
{
var url = $"http://xxx/api/environment/{id}";
this.environmentData = await httpClient.GetJsonAsync<EnvironmentData>(url);
this.StateHasChanged();
}
@TheRubble Since the comment you quoted was written, we did add proper support for async event handler. You just need to return a Task, not void, e.g.:
private async Task SwitchEnvironment(string id) { ... }
This also means you don't need the manual StateHasChanged call after the final await.
Most helpful comment
No, you are correct. We just haven't implemented
onclickAsyncyet. In the meantime you need to callStateHasChangedmanually.