# RedBus **Repository Path**: fujc2dev/RedBus ## Basic Information - **Project Name**: RedBus - **Description**: No description available - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2023-12-27 - **Last Updated**: 2023-12-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # RedBus A simple in memory EventBus / MessageBus library in C# with no dependencies [![Build status](https://ci.appveyor.com/api/projects/status/aeoqsovbx5k7vfxg?svg=true)](https://ci.appveyor.com/project/mxjones/redbus) # Nuget ``` Install-Package Redbus ``` [![NuGet](https://img.shields.io/nuget/v/Redbus.svg)](https://www.nuget.org/packages/Redbus/) # Supported Frameworks * .NET Framework 4.5 * .NET Standard 2.0 # Example Usage There is a generic PayloadEvent class that can be used, or you can use any custom classes that derive from EventBase ```c# private void TestMethod() { IEventBus eventBus = new EventBus(); eventBus.Subscribe>(OnIntEvent); eventBus.Subscribe(OnCustomEvent); eventBus.Publish(new PayloadEvent(5)); // OnIntEvent will be invoked eventBus.Publish(new CustomEventClass()); // OnCustomEvent will be invoked eventBus.Subscribe>(s => { Console.WriteLine(s.Payload); }); eventBus.Publish(new PayloadEvent("Hello")); } private void OnCustomEvent(CustomEventClass customEvent) { Console.WriteLine("Received CustomEvent"); } private void OnIntEvent(PayloadEvent intEvent) { Console.WriteLine(intEvent.Payload); } ``` Note that RedBus does not have WeakReferences implemented, so you must explicitly call Unsubscribe for the event when disposing the subscriber to avoid memory leaks. The Subscribe method returns a SubscriptionToken, this is used when unsubscribing. ```csharp IEventBus eventBus = new EventBus(); var token = eventBus.Subscribe>(s => { Console.WriteLine(s.Payload); }); eventBus.Unsubscribe(token); ``` # Extensions ```csharp IEventBus eventBus = new EventBus(); var token = eventBus.Subscribe>(s => { Console.WriteLine(s.Payload); }); token.Unsubscribe(eventBus) var payloadEvent = new PayloadEvent("Hello"); payloadEvent.Publish(eventBus); ``` # Configuration * ThrowSubscriberException By default, Redbus will catch and swallow any exceptions thrown by subscribers. It's possible to configure Redbus to rethrow any exceptions which occurred in a subscriber method - please note the following: * This will cause an exception from a subscriber to be thrown for a publisher * This will/may cause subsequent subscribers to *not* receive an event/message after a subscriber has thrown an exception. ```csharp IEventBus eventBus = new EventBus(new EventBusConfiguration { ThrowSubscriberException = true }); ```