@@ -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
203202using System.Collections.Generic;
204203using System.Threading.Tasks;
205204using System;
206205using Microsoft.AspNetCore.Mvc;
207206using Dapr;
208207using 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
439435using System ;
440436using System .Collections .Generic ;
441437using System .Net .Http ;
442438using System .Net .Http .Headers ;
443439using System .Threading .Tasks ;
444440using Dapr .Client ;
445- using Microsoft .AspNetCore .Mvc ;
446441using 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