Skip to content

Commit 79d0320

Browse files
authored
Merge branch 'v1.15' into issue_4410
2 parents 8cbd37a + dd0fd38 commit 79d0320

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

daprdocs/content/en/developing-applications/building-blocks/pubsub/subscription-methods.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,55 @@ As messages are sent to the given message handler code, there is no concept of r
203203

204204
The example below shows the different ways to stream subscribe to a topic.
205205

206-
{{< tabs Python Go >}}
206+
{{< tabs ".NET" Python Go >}}
207+
208+
{{% codetab %}}
209+
210+
You can use the `SubscribeAsync` method on the `DaprPublishSubscribeClient` to configure the message handler to use to pull messages from the stream.
211+
212+
```c#
213+
using System.Text;
214+
using Dapr.Messaging.PublishSubscribe;
215+
using Dapr.Messaging.PublishSubscribe.Extensions;
216+
217+
var builder = WebApplication.CreateBuilder(args);
218+
builder.Services.AddDaprPubSubClient();
219+
var app = builder.Build();
220+
221+
var messagingClient = app.Services.GetRequiredService<DaprPublishSubscribeClient>();
222+
223+
//Create a dynamic streaming subscription and subscribe with a timeout of 30 seconds and 10 seconds for message handling
224+
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
225+
var subscription = await messagingClient.SubscribeAsync("pubsub", "myTopic",
226+
new DaprSubscriptionOptions(new MessageHandlingPolicy(TimeSpan.FromSeconds(10), TopicResponseAction.Retry)),
227+
HandleMessageAsync, cancellationTokenSource.Token);
228+
229+
await Task.Delay(TimeSpan.FromMinutes(1));
230+
231+
//When you're done with the subscription, simply dispose of it
232+
await subscription.DisposeAsync();
233+
return;
234+
235+
//Process each message returned from the subscription
236+
Task<TopicResponseAction> HandleMessageAsync(TopicMessage message, CancellationToken cancellationToken = default)
237+
{
238+
try
239+
{
240+
//Do something with the message
241+
Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));
242+
return Task.FromResult(TopicResponseAction.Success);
243+
}
244+
catch
245+
{
246+
return Task.FromResult(TopicResponseAction.Retry);
247+
}
248+
}
249+
```
250+
251+
[Learn more about streaming subscriptions using the .NET SDK client.]({{< ref "dotnet-messaging-pubsub-howto.md" >}})
252+
253+
{{% /codetab %}}
254+
207255

208256
{{% codetab %}}
209257

0 commit comments

Comments
 (0)