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.
 
 
 

92 linhas
2.9 KiB

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading.Tasks;
  4. using DotNetCore.CAP;
  5. using Microsoft.AspNetCore.Authorization;
  6. using Microsoft.AspNetCore.Mvc;
  7. namespace Sample.RabbitMQ.SqlServer.Controllers
  8. {
  9. public class Person
  10. {
  11. public int Id { get; set; }
  12. public string Name { get; set; }
  13. public int Age { get; set; }
  14. public override string ToString()
  15. {
  16. return "Name:" + Name + ";Age:" + Age;
  17. }
  18. }
  19. [Route("api/[controller]")]
  20. public class ValuesController : Controller, ICapSubscribe
  21. {
  22. private readonly ICapPublisher _capBus;
  23. private readonly AppDbContext _dbContext;
  24. public ValuesController(ICapPublisher producer, AppDbContext dbContext)
  25. {
  26. _capBus = producer;
  27. _dbContext = dbContext;
  28. }
  29. [Route("~/publish")]
  30. public IActionResult PublishMessage()
  31. {
  32. _capBus.Publish("sample.rabbitmq.mysql22222", DateTime.Now);
  33. // _capBus.Publish("sample.rabbitmq.mysql33333", person);
  34. return Ok();
  35. }
  36. [Route("~/publishWithTrans")]
  37. public async Task<IActionResult> PublishMessageWithTransaction()
  38. {
  39. using (var trans = await _dbContext.Database.BeginTransactionAsync())
  40. {
  41. await _capBus.PublishAsync("sample.rabbitmq.mysql", "");
  42. trans.Commit();
  43. }
  44. return Ok();
  45. }
  46. [CapSubscribe("sample.rabbitmq.mysql33333")]
  47. public void KafkaTest22(Person person)
  48. {
  49. var aa = _dbContext.Database;
  50. _dbContext.Dispose();
  51. Console.WriteLine("[sample.kafka.sqlserver] message received " + person.ToString());
  52. Debug.WriteLine("[sample.kafka.sqlserver] message received " + person.ToString());
  53. }
  54. //[CapSubscribe("sample.rabbitmq.mysql22222")]
  55. //public void KafkaTest22(DateTime time)
  56. //{
  57. // Console.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  58. // Debug.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  59. //}
  60. [CapSubscribe("sample.rabbitmq.mysql22222")]
  61. public async Task<DateTime> KafkaTest33(DateTime time)
  62. {
  63. Console.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  64. Debug.WriteLine("[sample.kafka.sqlserver] message received " + time.ToString());
  65. return await Task.FromResult(time);
  66. }
  67. [NonAction]
  68. [CapSubscribe("sample.kafka.sqlserver3")]
  69. [CapSubscribe("sample.kafka.sqlserver4")]
  70. public void KafkaTest()
  71. {
  72. Console.WriteLine("[sample.kafka.sqlserver] message received");
  73. Debug.WriteLine("[sample.kafka.sqlserver] message received");
  74. }
  75. }
  76. }