-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorker.cs
More file actions
44 lines (31 loc) · 1.15 KB
/
Worker.cs
File metadata and controls
44 lines (31 loc) · 1.15 KB
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
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
namespace MessageConsumer;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
// Establish connection
var factory = new ConnectionFactory(){HostName= "localhost"};
var connection = factory.CreateConnection();
// To Create the communication channel.
IModel channel = connection.CreateModel();
string queue_name = "demo-queue";
channel.QueueDeclare(queue: queue_name, durable: true, exclusive: false,
autoDelete: false,arguments: null);
var _consumer = new EventingBasicConsumer(channel);
_consumer.Received += (p1, p2)=>{
var body = p2.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("Message Received: " + message);
};
channel.BasicConsume(queue: queue_name, autoAck: true, _consumer = _consumer);
}
}