implementing-pubsub-pattern

Implements Pub-Sub event communication in .NET using System.Reactive and System.Threading.Channels.

Updated Nov 22, 2023
One-click install
npx skills add https://github.com/parksanghoon-sys/TestCode --skill implementing-pubsub-pattern-parksanghoon-sys
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: implementing-pubsub-pattern
Source: https://github.com/parksanghoon-sys/TestCode/tree/main/src/Modbus/wpf-dev-pack/.agents/skills/implementing-pubsub-pattern
Command: npx skills add https://github.com/parksanghoon-sys/TestCode --skill implementing-pubsub-pattern-parksanghoon-sys

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires System.Reactive.

What problem does it solve? Decoupling components in .NET applications often leads to tangled event wiring, memory leaks from undisposed subscriptions, and uncontrolled message flow. This Skill provides proven patterns for building event-driven, loosely coupled architectures using Rx.NET and Channels. ## Core Features & Use Cases - Channels-based Producer-Consumer: Implement unbounded and bounded channels with backpressure control, worker pools, and DI registration. - Rx.NET Event Aggregator: Build a typed EventAggregator with Subject, plus operators like Throttle, Buffer, and Retry for reactive event streams. - Use Case: A WPF application needs to decouple its UI from background data services. Use this Skill to set up a Channel-based message bus with DI, or an Rx.NET EventAggregator so modules publish and subscribe to events like UserLoggedIn without direct references. ## Quick Start Ask the AI to implement a Pub-Sub event aggregator in .NET using System.Reactive or a Channel-based message bus for your producer-consumer scenario.

Frequently Asked Questions about implementing-pubsub-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a Pub-Sub event aggregator in .NET?▼

Use System.Reactive's Subject<T> wrapped in an EventAggregator class that exposes GetEvent<T>() for typed subscriptions and Publish<T>() for broadcasting events. Subscribers filter by event type using OfType<T>() and can apply operators like Where or Throttle.

How to use Channel<T> as a message bus in .NET?▼

Create a channel with Channel.CreateUnbounded<T>() or Channel.CreateBounded<T>(), write messages via Writer.WriteAsync, and consume them with await foreach over Reader.ReadAllAsync. Register the channel, reader, and writer as singletons in the DI container.

Channels vs Rx.NET: which should I use for event communication?▼

Channels suit producer-consumer scenarios with built-in backpressure and are part of the BCL with a low learning curve. Rx.NET fits event stream processing with rich operators like Throttle, Buffer, and Retry, but requires a NuGet dependency and steeper learning.

How do I prevent memory leaks with Rx.NET subscriptions?▼

Every Subscribe() call returns an IDisposable that must be disposed when no longer needed. Store the subscription and call Dispose() during cleanup, and dispose the Subject itself when the aggregator is shut down.

How do I handle backpressure with System.Threading.Channels?▼

Use Channel.CreateBounded with BoundedChannelOptions to cap buffer size. Set FullMode to Wait to block writers, or DropOldest to discard old messages, preventing unbounded memory growth when consumers fall behind.

Is Subject<T> in Rx.NET thread-safe?▼

No, Subject<T> is not thread-safe by default. If multiple threads publish concurrently, wrap the subject with Synchronize() to serialize OnNext calls, or use Channels which are thread-safe by default.