Skip to content

[Bug] Toasts not shown when using IToastService from a DelegatingHandler in Blazor WASM #272

@Noveboi

Description

@Noveboi

Describe the bug
When invoking any IToastService.ShowXYZ method from a DelegatingHandler in a Blazor WebAssembly environment, the result is that no toast is shown in the UI.

To Reproduce
Steps to reproduce the behavior:

  1. Clone the minimal repro repository: https://github.com/Noveboi/BlazoredToastDelegatingHandlers
  2. Run the app
  3. Click the "Show Toast" button to ensure that toasts are shown in a regular scenario
  4. Click the "Call HttpClient" button to see that no toasts are shown
  5. Open browser console to find the log that shows the DelegatingHandler is actually being invoked.
Image

Expected behavior
Two info toasts should appear, one when the request is sent and one when the response is received. Take a look at the DelegatingHandler's code to see exactly what should happen:

internal sealed class HttpNotificationMiddleware(
    IToastService toast,
    ILogger<HttpNotificationMiddleware> log) : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        log.LogInformation("To verify the handler is actually being run!");
        toast.ShowInfo("Sending request!");
        
        var response = await base.SendAsync(request, cancellationToken);
        
        toast.ShowInfo("Got response!");
        return response;
    }
}

Hosting Model (is this issue happening with a certain hosting model?):
Blazor WebAssembly

Additional context
I will post comments if I find anything useful

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugSomething isn't workingTriageIssue needs to be triaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions