Skip to content

Commit 61e23e0

Browse files
WhitWaldomsfussell
andauthored
Updated to modernize .NET examples (#4631)
Signed-off-by: Whit Waldo <[email protected]> Co-authored-by: Mark Fussell <[email protected]>
1 parent f4d95a4 commit 61e23e0

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

daprdocs/content/en/developing-applications/building-blocks/pubsub/howto-publish-subscribe.md

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -199,27 +199,24 @@ Below are code examples that leverage Dapr SDKs to subscribe to the topic you de
199199
{{% codetab %}}
200200

201201
```csharp
202-
//dependencies
203202
using System.Collections.Generic;
204203
using System.Threading.Tasks;
205204
using System;
206205
using Microsoft.AspNetCore.Mvc;
207206
using Dapr;
208207
using Dapr.Client;
209208
210-
//code
211-
namespace CheckoutService.controller
209+
namespace CheckoutService.Controllers;
210+
211+
[ApiController]
212+
public sealed class CheckoutServiceController : ControllerBase
212213
{
213-
[ApiController]
214-
public class CheckoutServiceController : Controller
214+
//Subscribe to a topic called "orders" from the "order-pub-sub" compoennt
215+
[Topic("order-pub-sub", "orders")]
216+
[HttpPost("checkout")]
217+
public void GetCheckout([FromBody] int orderId)
215218
{
216-
//Subscribe to a topic
217-
[Topic("order-pub-sub", "orders")]
218-
[HttpPost("checkout")]
219-
public void getCheckout([FromBody] int orderId)
220-
{
221-
Console.WriteLine("Subscriber received : " + orderId);
222-
}
219+
Console.WriteLine("Subscriber received : " + orderId);
223220
}
224221
}
225222
```
@@ -435,38 +432,34 @@ Below are code examples that leverage Dapr SDKs to publish a topic.
435432
{{% codetab %}}
436433

437434
```csharp
438-
//dependencies
439435
using System;
440436
using System.Collections.Generic;
441437
using System.Net.Http;
442438
using System.Net.Http.Headers;
443439
using System.Threading.Tasks;
444440
using Dapr.Client;
445-
using Microsoft.AspNetCore.Mvc;
446441
using System.Threading;
447442

448-
//code
449-
namespace EventService
450-
{
451-
class Program
452-
{
453-
static async Task Main(string[] args)
454-
{
455-
string PUBSUB_NAME = "order-pub-sub";
456-
string TOPIC_NAME = "orders";
457-
while(true) {
458-
System.Threading.Thread.Sleep(5000);
459-
Random random = new Random();
460-
int orderId = random.Next(1,1000);
461-
CancellationTokenSource source = new CancellationTokenSource();
462-
CancellationToken cancellationToken = source.Token;
463-
using var client = new DaprClientBuilder().Build();
464-
//Using Dapr SDK to publish a topic
465-
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
466-
Console.WriteLine("Published data: " + orderId);
467-
}
468-
}
469-
}
443+
const string PUBSUB_NAME = "order-pub-sub";
444+
const string TOPIC_NAME = "orders";
445+
446+
var builder = WebApplication.CreateBuilder(args);
447+
builder.Services.AddDaprClient();
448+
449+
var app = builder.Build();
450+
var random = new Random();
451+
452+
var client = app.Services.GetRequiredService<DaprClient>();
453+
454+
while(true) {
455+
await Task.Delay(TimeSpan.FromSeconds(5));
456+
var orderId = random.Next(1,1000);
457+
var source = new CancellationTokenSource();
458+
var cancellationToken = source.Token;
459+
460+
//Using Dapr SDK to publish a topic
461+
await client.PublishEventAsync(PUBSUB_NAME, TOPIC_NAME, orderId, cancellationToken);
462+
Console.WriteLine("Published data: " + orderId);
470463
}
471464
```
472465

0 commit comments

Comments
 (0)