cloudnet-compose/vote/dotnet/Vote/Messaging/MessageQueue.cs

36 lines
1.0 KiB
C#
Raw Permalink Normal View History

2018-09-21 20:23:31 +02:00
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using NATS.Client;
using Vote.Messaging.Messages;
namespace Vote.Messaging
{
public class MessageQueue : IMessageQueue
{
protected readonly IConfiguration _configuration;
protected readonly ILogger _logger;
public MessageQueue(IConfiguration configuration, ILogger<MessageQueue> logger)
{
_configuration = configuration;
_logger = logger;
}
public void Publish<TMessage>(TMessage message)
where TMessage : Message
{
using (var connection = CreateConnection())
{
var data = MessageHelper.ToData(message);
connection.Publish(message.Subject, data);
}
}
public IConnection CreateConnection()
{
var url = _configuration.GetValue<string>("MessageQueue:Url");
return new ConnectionFactory().CreateConnection(url);
}
}
}