Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

84 Zeilen
2.5 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using Dapper;
  4. using DotNetCore.CAP;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Data.SqlClient;
  7. namespace Sample.RabbitMQ.SqlServer.Controllers
  8. {
  9. [Route("api/[controller]")]
  10. public class ValuesController : Controller
  11. {
  12. private readonly ICapPublisher _capBus;
  13. public ValuesController(ICapPublisher capPublisher)
  14. {
  15. _capBus = capPublisher;
  16. }
  17. [Route("~/without/transaction")]
  18. public async Task<IActionResult> WithoutTransaction()
  19. {
  20. await _capBus.PublishAsync("sample.rabbitmq.sqlserver", new Person()
  21. {
  22. Id = 123,
  23. Name = "Bar"
  24. });
  25. return Ok();
  26. }
  27. [Route("~/adonet/transaction")]
  28. public IActionResult AdonetWithTransaction()
  29. {
  30. using (var connection = new SqlConnection(AppDbContext.ConnectionString))
  31. {
  32. using (var transaction = connection.BeginTransaction(_capBus, true))
  33. {
  34. //your business code
  35. connection.Execute("insert into test(name) values('test')", transaction: transaction);
  36. _capBus.Publish("sample.rabbitmq.sqlserver", new Person()
  37. {
  38. Id = 123,
  39. Name = "Bar"
  40. });
  41. }
  42. }
  43. return Ok();
  44. }
  45. [Route("~/ef/transaction")]
  46. public IActionResult EntityFrameworkWithTransaction([FromServices]AppDbContext dbContext)
  47. {
  48. using (dbContext.Database.BeginTransaction(_capBus, autoCommit: true))
  49. {
  50. dbContext.Persons.Add(new Person() { Name = "ef.transaction" });
  51. _capBus.Publish("sample.rabbitmq.sqlserver", new Person()
  52. {
  53. Id = 123,
  54. Name = "Bar"
  55. });
  56. }
  57. return Ok();
  58. }
  59. [NonAction]
  60. [CapSubscribe("sample.rabbitmq.sqlserver")]
  61. public void Subscriber(Person p)
  62. {
  63. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  64. }
  65. [NonAction]
  66. [CapSubscribe("sample.rabbitmq.sqlserver", Group = "group.test2")]
  67. public void Subscriber2(Person p, [FromCap]CapHeader header)
  68. {
  69. Console.WriteLine($@"{DateTime.Now} Subscriber invoked, Info: {p}");
  70. }
  71. }
  72. }