You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

47 line
1.3 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP;
  5. using Microsoft.AspNetCore.Mvc;
  6. namespace Sample.Kafka.Controllers
  7. {
  8. [Route("api/[controller]")]
  9. public class ValuesController : Controller, ICapSubscribe
  10. {
  11. private readonly ICapPublisher _capBus;
  12. private readonly AppDbContext _dbContext;
  13. public ValuesController(ICapPublisher producer, AppDbContext dbContext)
  14. {
  15. _capBus = producer;
  16. _dbContext = dbContext;
  17. }
  18. [Route("~/publish")]
  19. public IActionResult PublishMessage()
  20. {
  21. _capBus.Publish("sample.rabbitmq.mysql", "");
  22. return Ok();
  23. }
  24. [Route("~/publishWithTrans")]
  25. public async Task<IActionResult> PublishMessageWithTransaction()
  26. {
  27. using (var trans = await _dbContext.Database.BeginTransactionAsync())
  28. {
  29. await _capBus.PublishAsync("sample.rabbitmq.mysql", "");
  30. trans.Commit();
  31. }
  32. return Ok();
  33. }
  34. [NonAction]
  35. [CapSubscribe("sample.kafka.sqlserver", Group = "test")]
  36. public void KafkaTest()
  37. {
  38. Console.WriteLine("[sample.kafka.sqlserver] message received");
  39. Debug.WriteLine("[sample.kafka.sqlserver] message received");
  40. }
  41. }
  42. }