Skip to content

InvalidOperationException thrown when using the ToastService after asynchronous operations #836

Open
@Suiram1701

Description

@Suiram1701

Describe the bug
When you use the ToatService after an asynchronous operation an InvalidOperationException is throwed: System.InvalidOperationException: The current thread is not associated with the Dispatcher. Use InvokeAsync() to switch execution to the Dispatcher when triggering rendering or component state.. The Toast that should be created using the ToastService appears as expected but with the exception.
It is possible to fix this issue if you wrap the ToastService call in a InvokeAsync(() => { }) call but it would be nice if I don't have to do this on every ToastService call.

To Reproduce

  1. Create a Blazor Server application
  2. Use interactive server as global rendermode
  3. Add in the App.razor or somewere else the Toasts component with the interactive server rendermode.
  4. In any page inject the ToastService
  5. Add a button to this page that performs on click an asynchronous operation and after this the ToastService should notify the user (I'am not sure why but not every async operation causes this issue. In my case often at Db queries).

Expected behavior
Using the ToastService after async operation without the risk of throwing an exception.

Versions:

  • .NET Version: .NET 8
  • BlazorBootstrap: 3.0.0-preview-3
  • Blazor WebAssembly / Server: Server
  • Blazor Interactive Render Mode: Interactive Server
  • Blazor Interactivity Location: Global

Desktop:

  • OS: Windows 11
  • Browser Chrome

Smartphone: Not tested on mobile device because it's a server-side issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions