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.
 
 
 

60 regels
1.6 KiB

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