140
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
{
channel.
ExchangeDeclare
(exchange: _brokerName,
type:
"direct"
);
string
message = JsonConvert.
SerializeObject
(@event);
var
body = Encoding.
UTF8
.
GetBytes
(message);
channel.
BasicPublish
(exchange: _brokerName,
routingKey: eventName,
basicProperties:
null
,
body: body);
}
}
}
The
actual code
of the Publish method in the eShopOnContainers application is
improved by using a
Polly
retry policy, which retries the task some times in case the RabbitMQ container is not ready. This
scenario can occur when docker-compose is starting the containers; for example, the RabbitMQ
container might start more slowly than the other containers.
As
mentioned earlier, there are many possible configurations in RabbitMQ,
so this code should be
used only for dev/test environments.
Dostları ilə paylaş: