@@ -203,7 +203,47 @@ As messages are sent to the given message handler code, there is no concept of r
203
203
204
204
The example below shows the different ways to stream subscribe to a topic.
205
205
206
- {{< tabs Go>}}
206
+ {{< tabs ".NET" Go>}}
207
+
208
+ {{% codetab %}}
209
+
210
+ ` ` ` csharp
211
+ using Dapr.Messaging.PublishSubscribe;
212
+
213
+ var clientBuilder = new DaprPublishSubscribeClientBuilder();
214
+ var daprMessagingClient = clientBuilder.Build();
215
+
216
+ async Task<TopicResponseAction> HandleMessage(TopicMessage message, CancellationToken cancellationToken = default)
217
+ {
218
+ try
219
+ {
220
+ //Do something with the message
221
+ Console.WriteLine(Encoding.UTF8.GetString(message.Data.Span));
222
+
223
+ return await Task.FromResult(TopicResponseAction.Success);
224
+ }
225
+ catch
226
+ {
227
+ return await Task.FromResult(TopicResponseAction.Retry);
228
+ }
229
+ }
230
+
231
+ //Create a dynamic streaming subscription
232
+ var subscription = daprMessagingClient.Register("pubsub", "myTopic",
233
+ new DaprSubscriptionOptions(new MessageHandlingPolicy(TimeSpan.FromSeconds(15), TopicResponseAction.Retry)),
234
+ HandleMessage, CancellationToken.None);
235
+
236
+ //Subscribe to messages on it with a timeout of 30 seconds
237
+ var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
238
+ await subscription.SubscribeAsync(cancellationTokenSource.Token);
239
+
240
+ await Task.Delay(TimeSpan.FromMinutes(1));
241
+
242
+ //When you're done with the subscription, simply dispose of it
243
+ await subscription.DisposeAsync();
244
+ ` ` `
245
+
246
+ {{% /codetab %}}
207
247
208
248
{{% codetab %}}
209
249
0 commit comments