Blog Logo
TAGS

Lightweight .NET in-memory pub/sub implementation using Channels

Hi, in this article I’ll present to you how you can easily kick-off implementing performant in-memory pub/sub pattern using Channel<T> for asynchronous processing within your .NET server. We will see what are Channels and how we can use them, create interfaces as building blocks of our library supporting pub/sub, implement these interfaces for in-memory event bus based on Channel<T>, provide a user with a fluent registration, provide a user with an interface to resolve scoped event metadata on the fly, and reflect on ways we can now leverage our interfaces and implement other libraries for distributed environment and event sourcing. The motivation behind this is to achieve asynchronous processing in an easy way through readily available native .NET resources via appropriate abstractions that allow us to scale in the future. Channels are .net standard implementation of pub/sub pattern and are available in the System.Threading.Channels NuGet package. Channels come in two flavors, bounded and unbounded, and they provide different configuration options. We can use channels to create a basic example channel that produces random numbers and subscribes to event bus changes. More documentation on channels can be found at the provided link. We will start off by introducing 4 service contracts and 2 entity models supporting our library.