25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValuesController.cs 1.5 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 Npgsql;
  8. namespace Sample.Kafka.PostgreSql.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.kafka.postgrsql", DateTime.Now);
  22. return Ok();
  23. }
  24. [Route("~/adonet/transaction")]
  25. public IActionResult AdonetWithTransaction()
  26. {
  27. using (var connection = new NpgsqlConnection(""))
  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. _capBus.Publish("sample.kafka.postgrsql", DateTime.Now);
  34. transaction.Commit();
  35. }
  36. }
  37. return Ok();
  38. }
  39. [CapSubscribe("sample.kafka.postgrsql")]
  40. public void Test2(DateTime value)
  41. {
  42. Console.WriteLine("Subscriber output message: " + value);
  43. }
  44. }
  45. }