Skip to content

Commit

Permalink
Updated example to use DI registration
Browse files Browse the repository at this point in the history
Signed-off-by: Whit Waldo <[email protected]>
  • Loading branch information
WhitWaldo committed Oct 30, 2024
1 parent 2a51b6e commit 8febaa5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System.Text;
using Dapr.Messaging.PublishSubscribe;
using Dapr.Messaging.PublishSubscribe.Extensions;

var daprMessagingClientBuilder = new DaprPublishSubscribeClientBuilder(null);
var daprMessagingClient = daprMessagingClientBuilder.Build();
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDaprPubSubClient();
var app = builder.Build();

//Process each message returned from the subscription
Task<TopicResponseAction> HandleMessageAsync(TopicMessage message, CancellationToken cancellationToken = default)
Expand All @@ -19,9 +21,11 @@ Task<TopicResponseAction> HandleMessageAsync(TopicMessage message, CancellationT
}
}

//Create a dynamic streaming subscription and subscribe with a timeout of 20 seconds and 10 seconds for message handling
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(20));
var subscription = await daprMessagingClient.SubscribeAsync("pubsub", "myTopic",
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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down

0 comments on commit 8febaa5

Please sign in to comment.