본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Part 2: Declarative streaming subscriptions

원문 보기 위키 갱신

Consume pub/sub events over an application-initiated streaming gRPC connection The streaming example shows the recommended declarative subscription model for fixed topics.

출처: Dapr 문서

본문

Define a handler

Implement ITopicHandler<TMessage> and decorate the handler with DaprTopic:

[DaprTopic("pubsub", "orders", Delivery = DeliveryMode.Streaming)]
public sealed class OrderProcessingHandler : ITopicHandler<OrderPlaced>
{
public Task<TopicResponseAction> HandleAsync(
OrderPlaced order,
TopicContext context,
CancellationToken cancellationToken)
{
if (order.Quantity <= 0 || string.IsNullOrWhiteSpace(order.ItemSku))
{
return Task.FromResult(TopicResponseAction.Drop);
}

if (order.ItemSku.StartsWith("RETRY-", StringComparison.OrdinalIgnoreCase))
{
return Task.FromResult(TopicResponseAction.Retry);
}

return Task.FromResult(TopicResponseAction.Success);
}
}

Success acknowledges the message, Retry requests redelivery for a transient failure, and Drop rejects an invalid or poison message. The handler also receives TopicContext, which contains the pub/sub name, topic, message ID, headers, and metadata.

Configure the application

builder.Services.AddDaprMessaging();

The source generator discovers the attributed handlers, registers them in DI, and starts the streaming subscriber hosted service. Streaming is application-initiated, so the application does not need an inbound HTTP or gRPC endpoint for delivery. The SDK manages the stream and reconnects when the connection drops.

Test the handler and the stream

Unit tests construct handlers with mocked loggers and a small TopicContext, then assert Success, Retry, and Drop for representative inputs. This keeps validation and acknowledgement logic fast and independent of infrastructure. The integration test uses DaprTestApplicationBuilder and configures AddDaprMessaging with the harness’s gRPC endpoint. It publishes through the sidecar and waits on a test log sink for successful processing, inventory handling, and dropping an invalid order. This verifies the hosted service and real streaming delivery without requiring an application ingress port.

dotnet test examples\Messaging\02-Streaming\Streaming.Example02.Tests\Streaming.Example02.Tests.csproj

Next

  • Part 3: Content-based routing and dead-letter topics
  • Subscribe to topics how-to

더 알아보기 (Learn more)