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.
 
 
 

49 lines
1.3 KiB

  1. using System;
  2. using System.Threading.Tasks;
  3. using DotNetCore.CAP;
  4. using DotNetCore.CAP.Kafka;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Mvc;
  7. namespace Sample.Kafka.Controllers
  8. {
  9. [Route("api/[controller]")]
  10. public class ValuesController : Controller, ICapSubscribe
  11. {
  12. private readonly ICapPublisher _producer;
  13. public ValuesController(ICapPublisher producer)
  14. {
  15. _producer = producer;
  16. }
  17. [Route("/")]
  18. public IActionResult Index()
  19. {
  20. return Ok();
  21. }
  22. public string ServerPath => ((IHostingEnvironment)HttpContext.RequestServices.GetService(typeof(IHostingEnvironment))).ContentRootPath;
  23. [CapSubscribe("zzwl.topic.finace.callBack", Group = "test")]
  24. public void KafkaTest(Person person)
  25. {
  26. Console.WriteLine(person.Name);
  27. Console.WriteLine(person.Age);
  28. }
  29. [Route("~/send")]
  30. public async Task<IActionResult> SendTopic()
  31. {
  32. await _producer.PublishAsync("zzwl.topic.finace.callBack", new Person { Name = "Test", Age = 11 });
  33. return Ok();
  34. }
  35. public class Person
  36. {
  37. public string Name { get; set; }
  38. public int Age { get; set; }
  39. }
  40. }
  41. }