-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sender.cs
47 lines (43 loc) · 1.65 KB
/
Sender.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
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using RabbitMQ.Client;
namespace RabbitMQPractice;
public class Sender : BackgroundService
{
private readonly string _host;
private readonly string _virtualHost;
private readonly string _userName;
private readonly string _password;
public Sender(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,
Port = AmqpTcpEndpoint.UseDefaultPort
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null);
while (!stoppingToken.IsCancellationRequested)
{
string message = $"Now: {DateTime.Now}";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: string.Empty, routingKey: "hello", basicProperties: null, body: body);
await Task.Delay(1000,stoppingToken);
}
}
}