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.
 
 
 

55 lines
1.5 KiB

  1. using DotNetCore.CAP;
  2. using DotNetCore.CAP.Messages;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Microsoft.Extensions.Options;
  6. using System.Threading.Tasks;
  7. namespace Samples.Redis.SqlServer.Controllers
  8. {
  9. [ApiController]
  10. [Route("[controller]/[action]")]
  11. public class HomeController : ControllerBase
  12. {
  13. private readonly ILogger<HomeController> _logger;
  14. private readonly ICapPublisher _publisher;
  15. private readonly IOptions<CapOptions> _options;
  16. public HomeController(ILogger<HomeController> logger, ICapPublisher publisher, IOptions<CapOptions> options)
  17. {
  18. _logger = logger;
  19. _publisher = publisher;
  20. this._options = options;
  21. }
  22. [HttpGet]
  23. public async Task Publish([FromQuery] string message = "test-message")
  24. {
  25. await _publisher.PublishAsync(message, new Person() { Age = 11, Name = "James" });
  26. }
  27. [CapSubscribe("test-message")]
  28. [CapSubscribe("test-message-1")]
  29. [CapSubscribe("test-message-2")]
  30. [CapSubscribe("test-message-3")]
  31. [NonAction]
  32. public void Subscribe(Person p, [FromCap] CapHeader header)
  33. {
  34. _logger.LogInformation($"{header[Headers.MessageName]} subscribed with value --> " + p);
  35. }
  36. }
  37. public class Person
  38. {
  39. public string Name { get; set; }
  40. public int Age { get; set; }
  41. public override string ToString()
  42. {
  43. return "Name:" + Name + ", Age:" + Age;
  44. }
  45. }
  46. }