25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

55 satır
1.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 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. }