mirror of https://github.com/dapr/docs.git
Added .NET streaming subscription example to pubsub page
Signed-off-by: Whit Waldo <whit.waldo@innovian.net>
This commit is contained in:
parent
b24527ed5e
commit
6e990e4312
|
@ -203,7 +203,55 @@ As messages are sent to the given message handler code, there is no concept of r
|
|||
|
||||
The example below shows the different ways to stream subscribe to a topic.
|
||||
|
||||
{{< tabs Python Go >}}
|
||||
{{< tabs .NET Python Go >}}
|
||||
|
||||
{{% codetab %}}
|
||||
|
||||
You can use the `SubscribeAsync` method on the `DaprPublishSubscribeClient` to configure the message handler to use to pull messages from the stream.
|
||||
|
||||
```c#
|
||||
using System.Text;
|
||||
using Dapr.Messaging.PublishSubscribe;
|
||||
using Dapr.Messaging.PublishSubscribe.Extensions;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
builder.Services.AddDaprPubSubClient();
|
||||
var app = builder.Build();
|
||||
|
||||
var messagingClient = app.Services.GetRequiredService<DaprPublishSubscribeClient>();
|
||||
|
||||
//Create a dynamic streaming subscription and subscribe with a timeout of 30 seconds and 10 seconds for message handling
|
||||
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
|
||||
var subscription = await messagingClient.SubscribeAsync("pubsub", "myTopic",
|
||||
new DaprSubscriptionOptions(new MessageHandlingPolicy(TimeSpan.FromSeconds(10), TopicResponseAction.Retry)),
|
||||
HandleMessageAsync, cancellationTokenSource.Token);
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(1));
|
||||
|
||||
//When you're done with the subscription, simply dispose of it
|
||||
await subscription.DisposeAsync();
|
||||
return;
|
||||
|
||||
//Process each message returned from the subscription
|
||||
Task<TopicResponseAction> HandleMessageAsync(TopicMessage message, CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
//Do something with the message
|
||||
Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));
|
||||
return Task.FromResult(TopicResponseAction.Success);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Task.FromResult(TopicResponseAction.Retry);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
[Learn more about streaming subscriptions using the .NET SDK client.]({{< ref "dotnet-messaging-pubsub-howto.md" >}})
|
||||
|
||||
{{% /codetab %}}
|
||||
|
||||
|
||||
{{% codetab %}}
|
||||
|
||||
|
|
Loading…
Reference in New Issue