Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

87 lignes
2.5 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.RabbitMQ.MySql.Controllers
  9. {
  10. [Route("api/[controller]")]
  11. public class ValuesController : Controller
  12. {
  13. private readonly ICapPublisher _capBus;
  14. public ValuesController(ICapPublisher capPublisher)
  15. {
  16. _capBus = capPublisher;
  17. }
  18. [Route("~/without/transaction")]
  19. public async Task<IActionResult> WithoutTransaction()
  20. {
  21. await _capBus.PublishAsync("sample.rabbitmq.mysql", new Person()
  22. {
  23. Id = 123,
  24. Name = "Bar"
  25. });
  26. return Ok();
  27. }
  28. [Route("~/adonet/transaction")]
  29. public IActionResult AdonetWithTransaction()
  30. {
  31. using (var connection = new MySqlConnection(AppDbContext.ConnectionString))
  32. {
  33. using (var transaction = connection.BeginTransaction(_capBus, true))
  34. {
  35. //your business code
  36. connection.Execute("insert into test(name) values('test')", transaction: (IDbTransaction)transaction.DbTransaction);
  37. //for (int i = 0; i < 5; i++)
  38. //{
  39. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  40. //}
  41. }
  42. }
  43. return Ok();
  44. }
  45. [Route("~/ef/transaction")]
  46. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  47. {
  48. using (var trans = dbContext.Database.BeginTransaction(_capBus, autoCommit: false))
  49. {
  50. dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
  51. for (int i = 0; i < 1; i++)
  52. {
  53. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  54. }
  55. dbContext.SaveChanges();
  56. trans.Commit();
  57. }
  58. return Ok();
  59. }
  60. [NonAction]
  61. [CapSubscribe("sample.rabbitmq.mysql")]
  62. public void Subscriber(DateTime p)
  63. {
  64. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  65. }
  66. [NonAction]
  67. [CapSubscribe("sample.rabbitmq.mysql", Group = "group.test2")]
  68. public void Subscriber2(DateTime p, [FromCap]CapHeader header)
  69. {
  70. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  71. }
  72. }
  73. }