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.
 
 
 

58 lines
1.6 KiB

  1. using System;
  2. using System.Data;
  3. using System.Threading.Tasks;
  4. using Dapper;
  5. using DotNetCore.CAP;
  6. using Microsoft.AspNetCore.Mvc;
  7. using MySql.Data.MySqlClient;
  8. namespace Sample.Kafka.MySql.Controllers
  9. {
  10. [Route("api/[controller]")]
  11. public class ValuesController : Controller, ICapSubscribe
  12. {
  13. private readonly ICapPublisher _capBus;
  14. public ValuesController(ICapPublisher producer)
  15. {
  16. _capBus = producer;
  17. }
  18. [Route("~/without/transaction")]
  19. public async Task<IActionResult> WithoutTransaction()
  20. {
  21. await _capBus.PublishAsync("sample.rabbitmq.mysql", DateTime.Now);
  22. return Ok();
  23. }
  24. [Route("~/adonet/transaction")]
  25. public IActionResult AdonetWithTransaction()
  26. {
  27. using (var connection = new MySqlConnection(""))
  28. {
  29. using (var transaction = connection.BeginTransaction(_capBus, autoCommit: false))
  30. {
  31. //your business code
  32. connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);
  33. for (int i = 0; i < 5; i++)
  34. {
  35. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  36. }
  37. transaction.Commit();
  38. }
  39. }
  40. return Ok();
  41. }
  42. [CapSubscribe("#.test2")]
  43. public void Test2(int value)
  44. {
  45. Console.WriteLine("Subscriber output message: " + value);
  46. }
  47. }
  48. }