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.

ValuesController.cs 1.1 KiB

7 年之前
7 年之前
7 年之前
6 年之前
12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP;
  4. using Microsoft.AspNetCore.Mvc;
  5. using MySql.Data.MySqlClient;
  6. namespace Sample.Kafka.MySql.Controllers
  7. {
  8. [Route("api/[controller]")]
  9. public class ValuesController : Controller, ICapSubscribe
  10. {
  11. private readonly ICapPublisher _capBus;
  12. public ValuesController(ICapPublisher producer)
  13. {
  14. _capBus = producer;
  15. }
  16. [Route("~/publish")]
  17. public async Task<IActionResult> PublishMessage()
  18. {
  19. using (var connection = new MySqlConnection("Server=192.168.10.110;Database=testcap;UserId=root;Password=123123;"))
  20. {
  21. connection.Open();
  22. var transaction = connection.BeginTransaction();
  23. //your business code here
  24. await _capBus.PublishAsync("xxx.xxx.test2", 123456, transaction);
  25. transaction.Commit();
  26. }
  27. return Ok("publish successful!");
  28. }
  29. [CapSubscribe("#.test2")]
  30. public void Test2(int value)
  31. {
  32. Console.WriteLine("Subscriber output message: " + value);
  33. }
  34. }
  35. }