NetFabric/intelligentium / plugins/dotnet/.apm/skills/dotnet-channels
dotnet-channels
VerifiedDesign, implement, debug, test, and review in-process producer-consumer pipelines with System.Threading.Channels. USE FOR: Channel<T>, ChannelReader<T>, ChannelWriter<T>, bounded/unbounded queues, BoundedChannelFullMode, backpressure, TryWrite, WriteAsync, ReadAsync, ReadAllAsync, completion, cancellation, concurrent producers/consumers, BackgroundService workers, in-memory event queues, overload protection, and channel performance. DO NOT USE FOR: durable or cross-process messaging, distributed pub/sub, broker delivery guarantees, Rx.NET stream composition, task-parallel dataflow blocks, or simple thread-safe collections with no asynchronous producer-consumer requirement.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 4.3 KB |
| references/bounded-capacity-and-backpressure.md | markdown | 2.5 KB |
| references/concurrency-and-consumption.md | markdown | 2.4 KB |
| references/contracts-and-api.md | markdown | 3.0 KB |
| references/hosted-services-and-di.md | markdown | 2.1 KB |
| references/in-memory-messaging-boundaries.md | markdown | 1.8 KB |
| references/sources.md | markdown | 1.6 KB |
| references/testing-and-review.md | markdown | 2.2 KB |
references/hosted-services-and-di.md
View on GitHub# Hosted Services And Dependency Injection
## Queue Shape
- Register the queue owner as a singleton. Expose an application-specific enqueue interface rather than raw channel internals at public boundaries.
- Inject the queue's reader into one or more `BackgroundService` consumers. Start each consumer through `AddHostedService`.
- A hosted service is a singleton. Use `IServiceScopeFactory.CreateScope()` or `CreateAsyncScope()` per item or batch when resolving scoped dependencies such as database contexts.
- Pass the host's `stoppingToken` to `ReadAllAsync` and all processing operations. Define whether shutdown drains existing work or stops immediately.
## Consumer Pattern
```csharp
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await foreach (WorkItem item in reader.ReadAllAsync(stoppingToken))
{
await using AsyncServiceScope scope = scopeFactory.CreateAsyncScope();
IWorkHandler handler = scope.ServiceProvider.GetRequiredService<IWorkHandler>();
await handler.HandleAsync(item, stoppingToken);
}
}
```
- Keep retries bounded and cancellation-aware. Do not retry an item forever inside one worker.
- Catch expected per-item failures at the item boundary, log structured context without sensitive data, and send failures to a deliberate retry/dead-letter/persistence path.
- Let unexpected infrastructure failures end the service when fail-fast supervision is appropriate; do not silently swallow them.
## Shutdown
- Stop admitting new work before completion. Call `TryComplete` from the component that owns producer shutdown.
- The default host shutdown token may cancel before the channel drains. Configure a suitable shutdown timeout and make the drain policy explicit.
- Do not use a process-local channel for work that cannot be lost during restart. Persist it transactionally or use a durable broker/outbox.
## Sources
- [Worker services](https://learn.microsoft.com/dotnet/core/extensions/workers)
- [Dependency injection](https://learn.microsoft.com/dotnet/core/extensions/dependency-injection)
- [Channels](https://learn.microsoft.com/dotnet/core/extensions/channels)
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |