-
Notifications
You must be signed in to change notification settings - Fork 0
/
Receiver.cs
55 lines (47 loc) · 1.75 KB
/
Receiver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
namespace RabbitMQPractice;
public class Receiver : BackgroundService
{
private readonly string _host;
private readonly string _virtualHost;
private readonly string _userName;
private readonly string _password;
public Receiver(IConfiguration configuration)
{
_host = configuration.GetValue<string>("RabbitMQ:Host");
_virtualHost = configuration.GetValue<string>("RabbitMQ:VirtualHost");
_userName = configuration.GetValue<string>("RabbitMQ:UserName");
_password = configuration.GetValue<string>("RabbitMQ:Password");
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var factory = new ConnectionFactory
{
HostName = _host,
VirtualHost = _virtualHost,
UserName = _userName,
Password = _password
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "hello2", durable: true, exclusive: false, autoDelete: false, arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] Received {message}");
};
channel.BasicConsume(queue: "hello2",
autoAck: true,
consumer: consumer);
await Task.Delay(-1, stoppingToken);
}
}