Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ValuesController.cs 1.5 KiB

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