You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 7 години
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using DotNetCore.CAP;
  7. using Microsoft.AspNetCore.Mvc;
  8. namespace Sample.RabbitMQ.MySql.Controllers
  9. {
  10. [Route("api/[controller]")]
  11. public class ValuesController : Controller
  12. {
  13. private readonly AppDbContext _dbContext;
  14. private readonly ICapPublisher _capBus;
  15. public ValuesController(AppDbContext dbContext, ICapPublisher capPublisher)
  16. {
  17. _dbContext = dbContext;
  18. _capBus = capPublisher;
  19. }
  20. [Route("~/publish")]
  21. public IActionResult PublishMessage()
  22. {
  23. _capBus.Publish("sample.kafka.sqlserver", "");
  24. return Ok();
  25. }
  26. [Route("~/publishWithTrans")]
  27. public async Task<IActionResult> PublishMessageWithTransaction()
  28. {
  29. using (var trans = await _dbContext.Database.BeginTransactionAsync())
  30. {
  31. await _capBus.PublishAsync("sample.kafka.sqlserver", "");
  32. trans.Commit();
  33. }
  34. return Ok();
  35. }
  36. [NonAction]
  37. [CapSubscribe("sample.rabbitmq.mysql")]
  38. public void ReceiveMessage()
  39. {
  40. Console.WriteLine("[sample.rabbitmq.mysql] message received");
  41. Debug.WriteLine("[sample.rabbitmq.mysql] message received");
  42. }
  43. }
  44. }