Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

70 righe
2.0 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.AzureServiceBus.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.azure.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("sample.azure.mysql")]
  43. public void Test2(DateTime value)
  44. {
  45. Console.WriteLine("Subscriber output message: " + value);
  46. }
  47. [CapSubscribe("sample.azure.mysql")]
  48. public void Test2T2(DateTime value)
  49. {
  50. Console.WriteLine("Test2T2-->Subscriber output message: " + value);
  51. }
  52. [CapSubscribe("sample.azure.mysql",Group = "groupd")]
  53. public void Test2Group(DateTime value)
  54. {
  55. Console.WriteLine("Group--> Subscriber output message: " + value);
  56. }
  57. }
  58. }