-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathRabbitMqConnectionFactory.cs
37 lines (32 loc) · 1.33 KB
/
RabbitMqConnectionFactory.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
using System.Diagnostics.CodeAnalysis;
using RabbitMQ.Client;
using ViennaNET.Utils;
namespace ViennaNET.Messaging.RabbitMQQueue
{
[ExcludeFromCodeCoverage]
internal class RabbitMqConnectionFactory : IRabbitMqConnectionFactory
{
private const string DefaultHost = "localhost";
public IConnection Create(RabbitMqQueueConfiguration config)
{
config.ThrowIfNull(nameof(config));
var connectionFactory = new ConnectionFactory
{
VirtualHost = string.IsNullOrWhiteSpace(config.VirtualHost)
? ConnectionFactory.DefaultVHost
: config.VirtualHost,
UserName = string.IsNullOrWhiteSpace(config.User) ? ConnectionFactory.DefaultUser : config.User,
Password = string.IsNullOrWhiteSpace(config.Password) ? ConnectionFactory.DefaultPass : config.Password,
DispatchConsumersAsync = true,
RequestedConnectionTimeout = config.ConnectionTimeout
};
if (!string.IsNullOrWhiteSpace(config.Addresses))
{
return connectionFactory.CreateConnection(AmqpTcpEndpoint.ParseMultiple(config.Addresses));
}
connectionFactory.HostName = string.IsNullOrWhiteSpace(config.Server) ? DefaultHost : config.Server;
connectionFactory.Port = config.Port ?? AmqpTcpEndpoint.UseDefaultPort;
return connectionFactory.CreateConnection();
}
}
}