-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailQueueConsumer.cs
More file actions
59 lines (48 loc) · 1.84 KB
/
EmailQueueConsumer.cs
File metadata and controls
59 lines (48 loc) · 1.84 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;
using System.Threading.Tasks;
namespace MusicLibraryApp
{
public class EmailQueueConsumer
{
private readonly IEmailService _emailService;
public EmailQueueConsumer(IEmailService emailService)
{
_emailService = emailService;
}
public void Consume()
{
var factory = new ConnectionFactory
{
HostName = "localhost",
UserName = "guest",
Password = "guest",
};
using (var connection = factory.CreateConnection())
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("email_queue", 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);
// mesajı parçalayıp email, subject ve message olarak ayırıyoruz
var parts = message.Split(';');
if (parts.Length == 3)
{
var email = parts[0];
var subject = parts[1];
var emailMessage = parts[2];
// Email servisimizi kullanarak email gönderiyoruz
_emailService.SendEmailAsync(email, subject, emailMessage).Wait();
}
channel.BasicAck(ea.DeliveryTag, false);
};
channel.BasicConsume(queue: "email_queue", autoAck: false, consumer: consumer);
}
}
}
}