Skip to content

Commit 3f719f2

Browse files
authored
Merge pull request #4358 from WhitWaldo/streaming-subscription-dotnet
Added .NET implementation to docs for streaming subscription support
2 parents 56d4298 + 30d16be commit 3f719f2

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

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

+41-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,47 @@ 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 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 %}}
207247

208248
{{% codetab %}}
209249

0 commit comments

Comments
 (0)