blazorblazor-server-sideeventhandler

can I perform async calls in an EditContext.OnFieldChanged


Inside an EditContext OnFieldChanged event, when I have one specific field change, I need to write the change to the database. This is a Blazor server app and I use Entity Framework.

Is it ok for this to be async? Both will it work ok (it seems to). And equally important, will the user see no delay or pause?

This works fine in my testing. But that doesn't mean it will work great with any browser under any load in every situation. So are there concerns around this?


Solution

  • OnFieldChanged is an old style event, and its parameters have no writable properties.

    So this is exactly what async void was intended for. When you want to update the UI (probably not) then add a StateHasChanged() at the end.

    Using async void here is the lesser evil compared to not awaiting anything you do inside the method.

    And it certainly beats doing any synchronous I/O because that would cause a delay for the user.