Guest User

Untitled

a guest
Feb 5th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. public class AmqpService
  2. {
  3. private readonly AmqpInfo amqpInfo;
  4. private readonly ConnectionFactory connectionFactory;
  5. private const string QueueName = "DemoQueue";
  6.  
  7. public AmqpService(IOptions<AmqpInfo> ampOptionsSnapshot)
  8. {
  9. amqpInfo = ampOptionsSnapshot.Value;
  10.  
  11. connectionFactory = new ConnectionFactory
  12. {
  13. UserName = amqpInfo.Username,
  14. Password = amqpInfo.Password,
  15. VirtualHost = amqpInfo.VirtualHost,
  16. HostName = amqpInfo.HostName,
  17. Uri = new Uri(amqpInfo.Uri)
  18. };
  19. }
  20.  
  21. public void PublishMessage(object message)
  22. {
  23. using (var conn = connectionFactory.CreateConnection())
  24. {
  25. using (var channel = conn.CreateModel())
  26. {
  27. channel.QueueDeclare(
  28. queue: QueueName,
  29. durable: false,
  30. exclusive: false,
  31. autoDelete: false,
  32. arguments: null
  33. );
  34.  
  35. var jsonPayload = JsonConvert.SerializeObject(message);
  36. var body = Encoding.UTF8.GetBytes(jsonPayload);
  37.  
  38. channel.BasicPublish(exchange: "",
  39. routingKey: QueueName,
  40. basicProperties: null,
  41. body: body
  42. );
  43. }
  44. }
  45. }
  46. }
Add Comment
Please, Sign In to add comment