288 lines
8.9 KiB
C#
288 lines
8.9 KiB
C#
using System;
|
|
using RabbitMQ.Client;
|
|
using xCommons.Extensions;
|
|
using xEventService.Configuration;
|
|
using xEventService.Models;
|
|
using xExceptions.Constants;
|
|
|
|
namespace xEventService.Extensions {
|
|
public static class ConfigurationExtensions {
|
|
/// <summary>
|
|
/// Create Connection Factory Based on
|
|
/// provided Configuration ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static ConnectionFactory GetConnectionFactory (this XEventServiceConfiguration source) {
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull ()) {
|
|
XException.InvalidConfiguration.Throw ();
|
|
}
|
|
|
|
//
|
|
// Create Connection Factory ...
|
|
var result = new ConnectionFactory {
|
|
Port = source.Port,
|
|
HostName = source.Server,
|
|
UserName = source.Username,
|
|
Password = source.Password
|
|
};
|
|
|
|
//
|
|
// Return Result ...
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a Connection to Server ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <returns></returns>
|
|
public static IConnection Connect (this XEventServiceConfiguration source) {
|
|
//
|
|
// Open Connection ...
|
|
IConnection result = null;
|
|
try {
|
|
//
|
|
// Retrieve Connection Factory ...
|
|
var connectionFactory = source.GetConnectionFactory ();
|
|
|
|
//
|
|
// Create Connection ...
|
|
result = connectionFactory.CreateConnection ();
|
|
} catch (Exception ex) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: Connection Failed, {ex.Message} ...");
|
|
|
|
//
|
|
XException.ActionFailed.Throw ();
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Queue Name based on Configurations ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="queueName"></param>
|
|
public static void ValidateQueue (
|
|
this XEventServiceConfiguration source,
|
|
string queueName
|
|
) {
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull ()) {
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Validate Queues ...
|
|
if ((source.Queues.IsNull () &&
|
|
!source.AllowDynamicQueues) ||
|
|
(!source.Queues.IsNull () &&
|
|
!source.Queues.Keys.HasChild () &&
|
|
!source.AllowDynamicQueues)) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: there isn't any configured Queue ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Check queueName not empty ...
|
|
if (queueName.IsNullOrEmpty ()) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: Invalid QueueName ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Check queueName Exists ...
|
|
if (!source.Queues.IsNull () &&
|
|
!source.Queues.ContainsKey (queueName) &&
|
|
!source.AllowDynamicQueues) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: QueueName {queueName} not found ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Validate a Exchange Name based on Configurations ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="exchangeName"></param>
|
|
public static void ValidateExchange (
|
|
this XEventServiceConfiguration source,
|
|
string exchangeName
|
|
) {
|
|
//
|
|
// Validate Args ...
|
|
if (source.IsNull ()) {
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Validate Exchanges ...
|
|
if ((source.Exchanges.IsNull () &&
|
|
!source.AllowDynamicExchangess) ||
|
|
(!source.Exchanges.IsNull () &&
|
|
!source.Exchanges.Keys.HasChild () &&
|
|
!source.AllowDynamicExchangess)) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: there isn't any configured Exchanges ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Check exchangeName not empty ...
|
|
if (exchangeName.IsNullOrEmpty ()) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: Invalid ExchangeName ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
|
|
//
|
|
// Check exchangeName Exists ...
|
|
if (!source.Exchanges.IsNull () &&
|
|
!source.Exchanges.ContainsKey (exchangeName) &&
|
|
!source.AllowDynamicExchangess) {
|
|
//
|
|
Console.WriteLine ($"XEventService Exception: ExchangeName {exchangeName} not found ...");
|
|
|
|
//
|
|
XException.InvalidArgs.Throw ();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve specific QueueArgs ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="queueName"></param>
|
|
/// <returns></returns>
|
|
public static XQueueArgs GetQueueArgs (
|
|
this XEventServiceConfiguration source,
|
|
string queueName
|
|
) {
|
|
//
|
|
// Validate Queue Name ...
|
|
source.ValidateQueue (queueName);
|
|
|
|
//
|
|
var result = source.Queues
|
|
.IsNull () ? null : source
|
|
.Queues[queueName];
|
|
if (result.IsNull ()) {
|
|
//
|
|
// Prepare Default Queue ...
|
|
result = source.DefaultQueueArgs;
|
|
if (result.IsNull ()) {
|
|
result = new XQueueArgs () {
|
|
Name = queueName
|
|
};
|
|
} else {
|
|
result.Name = queueName;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieve specific ExchangeArgs ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="exchangeName"></param>
|
|
/// <returns></returns>
|
|
public static XExchangeArgs GetExchangeArgs (
|
|
this XEventServiceConfiguration source,
|
|
string exchangeName
|
|
) {
|
|
//
|
|
// Validate Exchange Name ...
|
|
source.ValidateExchange (exchangeName);
|
|
|
|
//
|
|
var result = source.Exchanges
|
|
.IsNull () ? null : source
|
|
.Exchanges[exchangeName];
|
|
if (result.IsNull ()) {
|
|
//
|
|
// Prepare Default Queue ...
|
|
result = source.DefaultExchangeArgs;
|
|
if (result.IsNull ()) {
|
|
result = new XExchangeArgs () {
|
|
Name = exchangeName,
|
|
Type = ExchangeType.Fanout
|
|
};
|
|
} else {
|
|
result.Name = exchangeName;
|
|
}
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a Channel ...
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="exchangeName"></param>
|
|
/// <returns></returns>
|
|
public static XChannel CreateChannel (
|
|
this XEventServiceConfiguration source,
|
|
string exchangeName = null
|
|
) {
|
|
//
|
|
// Validate ExchangeName and Retrieve ExchangeArgs if provided ...
|
|
var exchangeArgs = exchangeName
|
|
.IsNullOrEmpty () ?
|
|
null :
|
|
source.GetExchangeArgs (exchangeName);
|
|
|
|
//
|
|
// Create Connection ...
|
|
var connection = source.Connect ();
|
|
var channel = connection.CreateModel ();
|
|
|
|
//
|
|
// Create XChannel Model ...
|
|
var result = new XChannel (
|
|
exchangeName: exchangeName,
|
|
connection: connection
|
|
);
|
|
|
|
//
|
|
// Declaring Queue if Provided ...
|
|
if (!exchangeArgs.IsNull () &&
|
|
!exchangeName.IsNullOrEmpty ()) {
|
|
//
|
|
// Declaring Queue ...
|
|
result.Channel.ExchangeDeclare (
|
|
exchange: exchangeName,
|
|
type: exchangeArgs.Type,
|
|
durable: exchangeArgs.Durable,
|
|
autoDelete: exchangeArgs.AutoDelete
|
|
);
|
|
}
|
|
|
|
//
|
|
return result;
|
|
}
|
|
}
|
|
} |