Part 4: Bulk subscriptions
The bulk subscription example combines bulk publishing with a subscription configured for high-throughput ingestion.
Configure batching
Set BulkSubscribe and its limits on the topic attribute:
[DaprTopic(
"pubsub",
"telemetry",
BulkSubscribe = true,
MaxMessagesCount = 50,
MaxAwaitDurationMs = 500)]
public sealed class TelemetryBulkHandler : ITopicHandler<DeviceTelemetry>
{
public Task<TopicResponseAction> HandleAsync(
DeviceTelemetry reading,
TopicContext context,
CancellationToken cancellationToken)
{
if (reading.TemperatureCelsius < -273.15 ||
reading.HumidityPercent is < 0 or > 100)
{
return Task.FromResult(TopicResponseAction.Drop);
}
return Task.FromResult(TopicResponseAction.Success);
}
}
MaxMessagesCount bounds the batch size and MaxAwaitDurationMs bounds how long Dapr waits before delivering a partial batch. Choose values based on throughput, latency, and broker behavior. The handler remains responsible for the individual message decision.
Publish single events or batches
await client.PublishEventAsync("pubsub", "telemetry", reading, cancellationToken);
var response = await client.BulkPublishEventAsync(
"pubsub",
"telemetry",
readings,
options: null,
cancellationToken: cancellationToken);
Inspect response.FailedEntries when bulk publishing because a batch can contain partial failures.
Test batching-related behavior
The unit tests cover valid telemetry and invalid temperature and humidity values. The integration tests publish readings and batches through a real Dapr pub/sub harness to validate the generated bulk subscription configuration and delivery semantics.
dotnet test examples\Messaging\04-BulkSubscribe\BulkSubscribe.Example04.Tests\BulkSubscribe.Example04.Tests.csproj