Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ValuesController.cs 2.3 KiB

pirms 5 gadiem
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 Microsoft.Data.SqlClient;
  8. namespace Sample.RabbitMQ.SqlServer.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 SqlConnection(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: transaction);
  37. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  38. }
  39. }
  40. return Ok();
  41. }
  42. [Route("~/ef/transaction")]
  43. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  44. {
  45. using (dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  46. {
  47. dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
  48. _capBus.Publish("sample.rabbitmq.mysql", DateTime.Now);
  49. }
  50. return Ok();
  51. }
  52. [NonAction]
  53. [CapSubscribe("sample.rabbitmq.mysql")]
  54. public void Subscriber(DateTime p)
  55. {
  56. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  57. }
  58. [NonAction]
  59. [CapSubscribe("sample.rabbitmq.mysql", Group = "group.test2")]
  60. public void Subscriber2(DateTime p, [FromCap]CapHeader header)
  61. {
  62. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  63. }
  64. }
  65. }