Rewrite RabbitMQ ValuesController code to support netcoreapp2.1
This commit is contained in:
Родитель
2eb9f18622
Коммит
b8a50385f6
|
@ -120,37 +120,39 @@ namespace Company.WebApplication1.Controllers
|
|||
#if (RabbitMqOption)
|
||||
public ActionResult<string> Get()
|
||||
{
|
||||
using var connection = _factory.CreateConnection();
|
||||
using var channel = connection.CreateModel();
|
||||
// the queue
|
||||
channel.QueueDeclare(
|
||||
queue: QueueName,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
// consumer
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
consumer.Received += (model, ea) =>
|
||||
using (var connection = _factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
string msg = Encoding.UTF8.GetString(ea.Body);
|
||||
_logger.LogInformation("Received message: {Message}", msg);
|
||||
};
|
||||
channel.BasicConsume(
|
||||
queue: QueueName,
|
||||
autoAck: true,
|
||||
consumer: consumer);
|
||||
// publisher
|
||||
int i = 0;
|
||||
while (i < 5) // write a message every second, for 5 seconds
|
||||
{
|
||||
var body = Encoding.UTF8.GetBytes($"Message {++i}");
|
||||
channel.BasicPublish(
|
||||
exchange: "",
|
||||
routingKey: QueueName,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
Thread.Sleep(1000);
|
||||
// the queue
|
||||
channel.QueueDeclare(
|
||||
queue: QueueName,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
// consumer
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
consumer.Received += (model, ea) =>
|
||||
{
|
||||
string msg = Encoding.UTF8.GetString(ea.Body);
|
||||
_logger.LogInformation("Received message: {Message}", msg);
|
||||
};
|
||||
channel.BasicConsume(
|
||||
queue: QueueName,
|
||||
autoAck: true,
|
||||
consumer: consumer);
|
||||
// publisher
|
||||
int i = 0;
|
||||
while (i < 5) // write a message every second, for 5 seconds
|
||||
{
|
||||
var body = Encoding.UTF8.GetBytes($"Message {++i}");
|
||||
channel.BasicPublish(
|
||||
exchange: "",
|
||||
routingKey: QueueName,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
|
||||
return "Wrote 5 message to the info log. Have a look!";
|
||||
|
|
|
@ -58,37 +58,39 @@ public ValuesController(ILogger<ValuesController> logger, [FromServices] Connect
|
|||
[HttpGet]
|
||||
public ActionResult<string> Get()
|
||||
{
|
||||
using var connection = _factory.CreateConnection();
|
||||
using var channel = connection.CreateModel();
|
||||
// the queue
|
||||
channel.QueueDeclare(
|
||||
queue: QueueName,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
// consumer
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
consumer.Received += (model, ea) =>
|
||||
{
|
||||
string msg = Encoding.UTF8.GetString(ea.Body);
|
||||
_logger.LogInformation(""Received message: {Message}"", msg);
|
||||
};
|
||||
channel.BasicConsume(
|
||||
queue: QueueName,
|
||||
autoAck: true,
|
||||
consumer: consumer);
|
||||
// publisher
|
||||
int i = 0;
|
||||
while (i < 5) // write a message every second, for 5 seconds
|
||||
using (var connection = _factory.CreateConnection())
|
||||
using (var channel = connection.CreateModel())
|
||||
{
|
||||
var body = Encoding.UTF8.GetBytes($""Message {++i}"");
|
||||
channel.BasicPublish(
|
||||
exchange: """",
|
||||
routingKey: QueueName,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
Thread.Sleep(1000);
|
||||
// the queue
|
||||
channel.QueueDeclare(
|
||||
queue: QueueName,
|
||||
durable: false,
|
||||
exclusive: false,
|
||||
autoDelete: false,
|
||||
arguments: null);
|
||||
// consumer
|
||||
var consumer = new EventingBasicConsumer(channel);
|
||||
consumer.Received += (model, ea) =>
|
||||
{
|
||||
string msg = Encoding.UTF8.GetString(ea.Body);
|
||||
_logger.LogInformation(""Received message: {Message}"", msg);
|
||||
};
|
||||
channel.BasicConsume(
|
||||
queue: QueueName,
|
||||
autoAck: true,
|
||||
consumer: consumer);
|
||||
// publisher
|
||||
int i = 0;
|
||||
while (i < 5) // write a message every second, for 5 seconds
|
||||
{
|
||||
var body = Encoding.UTF8.GetBytes($""Message {++i}"");
|
||||
channel.BasicPublish(
|
||||
exchange: """",
|
||||
routingKey: QueueName,
|
||||
basicProperties: null,
|
||||
body: body);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
return ""Wrote 5 message to the info log. Have a look!"";
|
||||
");
|
||||
|
|
Загрузка…
Ссылка в новой задаче