Skip to content

NetFabric/intelligentium / plugins/dotnet/.apm/skills/dotnet-channels

dotnet-channels

Verified

Design, 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.

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)
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
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: 198kUpdated Claude CodeCodexVerifiedInstall