Event Aggregator for Blazor updated to 2.0.0
After some hiatus there’s now a new 2.0.0 version available of Event Aggregator for Blazor. For those who are not familiar with Event Aggregator for Blazor, Event aggregator is used for indirect component to component communication. In the event aggregator pattern you have message/event publishers and subscribers. In the case of Blazor, a component can publish its events and other component(s) can react to those.
Project home: https://github.com/mikoskinen/Blazor.EventAggregator
There’s few changes and additions in this release:
Easier initialization
Event Aggregator for Blazor now contains a ServiceCollectionExtension, making it easier to start using the component. From version 2.0.0 you can use the following initialization code in your ConfigureServices:
public void ConfigureServices(IServiceCollection services) { services.AddEventAggregator(); }
Note: The library has been tested only with the latest server side version of Blazor. It should work with Blazor WebAssembly, but more testing is required. In Blazor WebAssembly you register the library in application’s Main-method.
Auto refresh is disabled by default
Previously, Event Aggregator for Blazor tried always to execute subscriber’s StateHasChanged-method after it had handled a message. This made (and can still make) working with the Event Aggregator easier as it means you don’t have to manually call StateHasChanged in your Handle-method, if you want to update the UI. Unfortunately in some situations the auto refresh can cause performance issues so from version 2.0.0 onwards the auto refresh is disabled by default.
You can enable auto refresh through options:
services.AddEventAggregator(options => options.AutoRefresh = true);
Auto refresh is based on reflection and it assumes that the subscriber inherits from ComponentBase.
Auto refresh now understands component’s SynchronizationContext
Previously, Event Aggregator for Blazor’s auto refresh just executed component’s StateHasChanged-method. In the preview versions of Blazor this worked OK but as Blazor now uses SynchronizationContext to enforce a single logical thread of execution, auto refresh had a habit of crashing the application. This is now fixed.