For a modern .NET app, a practical in-process customer queue is a bounded Channel<T> with a hosted BackgroundService that consumes work asynchronously. Use a bounded queue with a deliberate full-queue policy, and treat this pattern as an in-memory handoff—not a guarantee that work survives a process failure or is coordinated across multiple app instances.
What a C# customer queue does
A customer queue separates accepting a request from performing its follow-up work. For example, an application can accept a customer operation, enqueue a work item, and let a background worker process that item. The queue contract should be clear about what a work item represents, when enqueueing is considered complete, how cancellation is handled, and what happens when capacity is reached.
Microsoft’s .NET queue-service tutorial demonstrates an IBackgroundTaskQueue abstraction with enqueue and dequeue operations. Its default implementation uses Channel<Func<CancellationToken, ValueTask>>, with a BackgroundService consuming the queued delegates. This is a useful starting pattern; it does not establish that the pattern meets a particular customer’s workload or service-level requirements.
How to queue background tasks in modern .NET
The outline below describes the roles in Microsoft’s queue-service pattern. It is an architectural sketch, not a complete copy-paste application: register the queue as a shared service, enqueue work through its abstraction, and have a hosted worker dequeue and await each item. Keep the delegate asynchronous and make it observe the token supplied to it.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
- Define the queue contract. Provide asynchronous enqueue and dequeue operations. An asynchronous enqueue should report completion only when the queue accepts the item; with a bounded channel in wait mode, that may mean waiting for space.
- Represent the work. Use an asynchronous work item, such as a function accepting a
CancellationTokenand returning aValueTask. Avoid treating a fire-and-forget callback as proof that the work has completed. - Choose a bounded capacity and full mode. Set the capacity in light of expected application load and concurrent access. With
BoundedChannelFullMode.Wait, writers wait asynchronously when the queue is full, applying backpressure instead of silently discarding work. - Run a hosted consumer. A
BackgroundServicereads queued work and awaits each item. Pass the worker’s cancellation token through to operations that can respond to cancellation. - Manage dependency lifetimes. If processing needs a scoped dependency, such as a database context, create and use a scope in the worker rather than capturing a scoped service in a long-lived hosted service. Microsoft’s hosted-services guidance includes a scoped-service-in-background-service pattern.
Choose what happens when the queue is full
A bounded queue limits pending work. Its full mode determines whether producers wait or work is discarded. Microsoft’s Channels documentation describes these options:
| Design choice | Behavior | When to consider it |
|---|---|---|
Bounded, Wait mode |
An asynchronous write waits until there is room. TryWrite instead returns false immediately if it cannot write. |
When retaining accepted work matters more than keeping producers completely unblocked. The application still needs to handle waiting, cancellation, and overload. |
| Bounded, drop newest | The newest queued item is discarded when the channel is full. | Only when the application can explicitly tolerate losing that newest item. |
| Bounded, drop oldest | The oldest queued item is discarded to make room. | Only when discarding older pending work is acceptable. |
| Bounded, drop write | The item currently being written is discarded when the channel is full. | Only when losing the new item is acceptable and the application handles that policy deliberately. |
| Unbounded | No fixed capacity limits pending items; work can accumulate if producers outpace consumers. | Only when the consequences of unbounded growth are acceptable for the application. |
For customer operations, a drop mode is not a safe default: it deliberately loses work. Pick one only when the workflow can tolerate that specific loss and callers or operators have an appropriate way to know about it. A bounded wait policy applies backpressure, but it does not by itself define an end-to-end delivery guarantee.
Rank #2
What the hosted worker guarantees—and what it does not
A hosted service is managed by the application host. During a graceful shutdown, the host uses cancellation; worker code should pass the token to work and allow operations to respond. However, an abrupt process failure can prevent graceful-stop operations from running. An in-memory channel therefore should not be described as guaranteeing that queued customer work survives process loss.
The cited in-process pattern also does not establish persistence across restarts or coordination among multiple application instances. If either is required, determine how the system will provide it before relying on this queue. Do not assume durable delivery, exactly-once processing, or cross-instance behavior from Channel<T> alone.
Which C# runtime does the queue API target?
Do not confuse a legacy .NET Framework API with the modern .NET hosted-worker pattern. HostingEnvironment.QueueBackgroundWorkItem belongs to System.Web.Hosting and is documented for .NET Framework 4.8.1. Microsoft’s modern .NET tutorial instead demonstrates a custom queue abstraction backed by Channels and consumed by a hosted worker.
Quick Recap
Best Value
Rank #4
Decisions to make before using it for customer work
- Workload: What does each item contain, how quickly can work arrive, and how many producers may enqueue concurrently?
- Capacity and overload: How much pending work can the app hold, and should producers wait, receive a failure, or use an explicitly acceptable drop policy?
- Cancellation: What should happen to work during graceful shutdown, and which operations can respond to cancellation?
- Data and dependencies: Does the item contain sensitive customer data, and does processing need scoped services such as a database context?
- Delivery requirements: Must work survive process restarts, be coordinated across instances, or meet a defined maximum delay? The in-process tutorial does not answer these deployment-specific questions.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




