Skip to content

fix: NullReferenceException in MessageService.ShowMessageBarAsync #4674

@JamesNK

Description

@JamesNK

🐛 Bug Report

I see about 30 NullReferenceException errors per week from MessageService.ShowMessageBarAsync in my app.

I have a component that references IMessageService and can call ShowMessageBarAsync. But this sometimes errors. I think the problem is ShowMessageBarAsync assumes that OnMessageItemsUpdatedAsync has been subscribed to:

await OnMessageItemsUpdatedAsync!.Invoke();

This happens in FluentMessageBarProvider.OnInitialize:

MessageService.OnMessageItemsUpdatedAsync += OnMessageItemsUpdatedHandlerAsync;

💻 Repro or Code Sample

🤔 Expected Behavior

No error.

😯 Current Behavior

There is a race between OnInitialize in the two controls causing NullReferenceExceptions.

💁 Possible Solution

MessageService doesn't call the event if it isn't registered?

Also, isn't AllMessages not thread safe? The lock around it just locks while accessing the property. A copy isn't being returned so a message could be added to it while the message bar provider is enumerating the collection, causing an error. Either a copy needs to be made here, or a copy is made each time the message list is modifed.

🔦 Context

🌍 Your Environment

  • OS & Device: [e.g. MacOS, iOS, Windows, Linux] on [iPhone 7, PC]
  • Browser [e.g. Microsoft Edge, Google Chrome, Apple Safari, Mozilla FireFox]
  • .NET and Fluent UI Blazor library Version [e.g. 8.0.2 and 4.4.1]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions