1

I have a Blazor page (server-side) that uses an async call to Azure Service Bus to get some data and print the result to the page. Everything works fine at first. However, if the user reloads the page, the page does not get re-rendered as intended.

Debugging with breakpoints shows that the model is updated, and the call to StateHasChanged is executed after the page reload; it just simply doesn't do anything. I was under the impression that InvokeAsync is supposed to always use the UI thread, but that doesn't seem to be the case after a page reload.

protected override async Task OnAfterRenderAsync(bool firstRender)
{
  // On page load. Service bus watcher is hooked up here
  await serviceBusHandler.WatchQueue("servicebus-queue", MessageHandler, async _ => await Task.CompletedTask);
}

// "request" method; calls a Service Bus method get a new value for our model
private async Task SaveAndGetAmount()
{
  IsLoading = true;

  // creation of Service Bus message omitted
  // post to "servicebus-queue" with message here
}

private async Task MessageHandler(ProcessSessionMessageEventArgs args)
{
  var result = ... // parsing of "args" message omitted

  await args.CompleteMessageAsync(args.Message); // mark Azure Functions message received
  
  await InvokeAsync(() =>
  {
    IsLoading = false;
    Model.Amount = result.Amount; // update page model here
    StateHasChanged(); // indicate to UI that the model has changed, and the page must re-render
  });

Additionally, I added a watch to the Model.Amount property and it's exhibiting some weird behavior. If it's set to say 23 on initial page load, the watch will always show 23 on my breakpoint in the SaveAndGetAmount method, as if it's never being updated by the MessageHandler method.

10
  • InvokeAsync isn't broken. The problem is somewhere else. Best is a minimal reproducible example but at least show how MessageHandler is called. Any async void methods in the picture?
    – H H
    Jan 18, 2022 at 19:10
  • Thanks; I added the call to MessageHandler. It's part of a Service Bus hookup.
    – Andrew
    Jan 18, 2022 at 19:25
  • There were a couple async void methods someone added but I don't think they are directly related to this code. When I replaced them with async Task it broke my loading spinner (the IsLoading variable in my question), annoyingly.
    – Andrew
    Jan 18, 2022 at 19:33
  • you should call WatchQueue on each OnAfterRenderAsync, just when firstRender is true. Henk Holterman is right. The problem is somewhere else. The code you have posted works
    – Liero
    Jan 18, 2022 at 19:36
  • I'm not sure I follow; WatchQueue does get called on each OnAfterRenderAsync; we ignore firstRender.
    – Andrew
    Jan 18, 2022 at 19:42

1 Answer 1

2

WatchQueue does get called on each OnAfterRenderAsync; we ignore firstRender

protected override async Task OnAfterRenderAsync(bool firstRender)
{
  // On page load. Service bus watcher is hooked up here
  await serviceBusHandler.WatchQueue("servicebus-queue", MessageHandler, async _ => await Task.CompletedTask);
}

Just a guess here but this means every (Re)Render adds a messagehandler. That is wrong but goes unnoticed as long as you're on the same instance of the page.

After a reload your messages are still handled on the old (defunct) page. That is what I think you see happening in the debugger.

If I'm right the solution is two-fold:

@implements IDisposable

protected override async Task OnAfterRenderAsync(bool firstRender)
{
  if (firstRender)  // only once per page
  {
    // On page load. Service bus watcher is hooked up here
    await serviceBusHandler.WatchQueue("servicebus-queue", MessageHandler, async _ => await Task.CompletedTask);
  }
}

public void Dispose()
{
  // somehow undo the WatchQueue 
}

If you can't unhook that handler then it's at the wrong place.

4
  • Yep, that was it. I guess we were creating too many of those handlers on page load.
    – Andrew
    Jan 18, 2022 at 20:34
  • Is there a reason why you are hooking up the serviceBusHandler in OnAfterRenderAsync rather than in OnInitialisedAsync? Jan 18, 2022 at 23:26
  • @MrCakaShaunCurtis The page doesn't work if we do that. I get an error like "... When prerendering is enabled, JavaScript interop calls can only be performed during the OnAfterRenderAsync lifecycle method."
    – Andrew
    Jan 19, 2022 at 15:44
  • @Andrew. No problem, if you're using JSInterop then your stuck with OnAfterRender and firstRender. Jan 19, 2022 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.