This commit is contained in:
2026-08-22 06:55:47 +03:30
7 changed files with 75 additions and 105 deletions
+12 -81
View File
@@ -18,7 +18,7 @@ namespace xEventService.DI
/// </summary> /// </summary>
/// <param name="source"></param> /// <param name="source"></param>
/// <returns></returns> /// <returns></returns>
public static XEventServiceConfiguration GetXEventServiceConfigurations( public static XEventServiceConfiguration GetXEventServiceConfiguration(
this IConfiguration source this IConfiguration source
) )
{ {
@@ -36,14 +36,14 @@ namespace xEventService.DI
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="configuration"></param> /// <param name="configuration"></param>
public static void AddXEventServiceConfigurations( public static void AddXEventServiceConfiguration(
this IServiceCollection services, this IServiceCollection services,
IConfiguration configuration IConfiguration configuration
) )
{ {
// //
var config = configuration.GetXEventServiceConfigurations(); var config = configuration.GetXEventServiceConfiguration();
services.AddXEventServiceConfigurations(config); services.AddXEventServiceConfiguration(config);
} }
/// <summary> /// <summary>
@@ -51,14 +51,20 @@ namespace xEventService.DI
/// </summary> /// </summary>
/// <param name="services"></param> /// <param name="services"></param>
/// <param name="configuration"></param> /// <param name="configuration"></param>
public static void AddXEventServiceConfigurations( public static void AddXEventServiceConfiguration(
this IServiceCollection services, this IServiceCollection services,
XEventServiceConfiguration configuration XEventServiceConfiguration configuration
) )
{ {
// //
// Validate ... // Validate ...
if (!configuration.IsValid()) var isValid = configuration.IsValid() &&
(configuration.Broker == XEventBroker.XKafka ||
(configuration.Broker == XEventBroker.XZeroMQ &&
configuration.ZeroMQ.IsValid()) ||
(configuration.Broker == XEventBroker.XRabbitMQ &&
configuration.RabbitMQ.IsValid()));
if (!isValid)
{ {
// //
Log("Registration Failed, due Invalid Configuration ..."); Log("Registration Failed, due Invalid Configuration ...");
@@ -70,25 +76,6 @@ namespace xEventService.DI
services.AddSingleton(configuration); services.AddSingleton(configuration);
} }
/// <summary>
/// Register Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
/// <param name="lifeTime"></param>
public static void AddXEventService(
this IServiceCollection source,
XEventServiceConfiguration configuration,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
source.AddEventService(
lifeTime: lifeTime,
configuration: configuration
);
}
// //
#region Private ... #region Private ...
/// <summary> /// <summary>
@@ -104,62 +91,6 @@ namespace xEventService.DI
{ {
Console.WriteLine($"{XLogTag} => {message}"); Console.WriteLine($"{XLogTag} => {message}");
} }
/// <summary>
/// Register Event Service ...
/// </summary>
/// <param name="source"></param>
/// <param name="configuration"></param>
/// <param name="lifeTime"></param>
private static void AddEventService(
this IServiceCollection source,
XEventServiceConfiguration configuration,
ServiceLifetime lifeTime = ServiceLifetime.Scoped
)
{
//
// Validate ...
if (!configuration.IsValid())
{
//
Log("Initialization Failed due Invalid Configuration ...");
XException.InvalidConfiguration.Throw();
}
//
// Register based on Broker ...
switch (configuration.Broker)
{
//
case XEventBroker.XKafka:
source.Add(
new ServiceDescriptor(typeof(IXKafkaProducerService), typeof(XKafkaProducerService), lifeTime)
);
break;
//
case XEventBroker.XZeroMQ:
source.Add(
new ServiceDescriptor(typeof(IXZeroMQProducerService), typeof(XZeroMQProducerService), lifeTime)
);
break;
//
case XEventBroker.XRabbitMQ:
source.Add(
new ServiceDescriptor(typeof(IXRabbitMQProducerService), typeof(XRabbitMQProducerService), lifeTime)
);
break;
}
//
source.Add(
new ServiceDescriptor(typeof(IXEventServiceProvider), typeof(XEventServiceProvider), lifeTime)
);
//
Log($"Service Registered Sucessfull, Provider: {configuration.Broker.GetStringValue()}");
}
#endregion #endregion
} }
} }
+1 -3
View File
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using xCommons.Extensions; using xCommons.Extensions;
@@ -15,6 +12,7 @@ namespace xEventService.Providers
{ {
public class XEventServiceProvider : IXEventServiceProvider public class XEventServiceProvider : IXEventServiceProvider
{ {
//
private readonly XEventServiceConfiguration configuration; private readonly XEventServiceConfiguration configuration;
private readonly IXKafkaProducerService kafkaProducerService; private readonly IXKafkaProducerService kafkaProducerService;
private readonly IXZeroMQProducerService zeroMQProducerService; private readonly IXZeroMQProducerService zeroMQProducerService;
+8 -3
View File
@@ -74,6 +74,7 @@ namespace xEventService.Providers
// //
// Subscribe to Topic ... // Subscribe to Topic ...
consumer.Subscribe(topic); consumer.Subscribe(topic);
logger.LogInformation($"Kafka Cosume Topic: {topic} ...");
// //
// Consuming ... // Consuming ...
@@ -100,18 +101,22 @@ namespace xEventService.Providers
{ {
// //
await DoWorkAsync( await DoWorkAsync(
payload: consumeResult.Message, payload: message,
cancellationToken: cancellationToken cancellationToken: cancellationToken
); );
} }
} }
} }
catch (ConsumeException) catch (ConsumeException ex)
{ {
//
logger.LogError($"Kafka Consumer Result Parsing Error: {ex.Message} ...");
await Task.Delay(1000, cancellationToken); await Task.Delay(1000, cancellationToken);
} }
catch (Exception) catch (Exception ex)
{ {
//
logger.LogError($"Kafka Consumer Result Parsing Error: {ex.Message} ...");
await Task.Delay(1000, cancellationToken); await Task.Delay(1000, cancellationToken);
} }
} }
+4 -2
View File
@@ -63,8 +63,8 @@ namespace xEventService.Providers
// //
result = await SendMessageAsync( result = await SendMessageAsync(
message: request.Message,
topic: request.Topic, topic: request.Topic,
message: request.Message,
cancellationToken: cancellationToken cancellationToken: cancellationToken
); );
@@ -111,8 +111,10 @@ namespace xEventService.Providers
// //
result = !response.IsNullOrDefault(); result = !response.IsNullOrDefault();
} }
catch catch (Exception ex)
{ {
//
logger.LogError($"Kafka Produce Error: {ex.Message} ...");
result = false; result = false;
} }
+23 -10
View File
@@ -51,7 +51,7 @@ namespace xEventService.Providers
// //
SetupConnection(); SetupConnection();
DeclareTopology(); DeclareTopology(topic);
consumer = new AsyncEventingBasicConsumer(channel); consumer = new AsyncEventingBasicConsumer(channel);
} }
@@ -68,6 +68,7 @@ namespace xEventService.Providers
// For Fix Blocking Synchronous ... // For Fix Blocking Synchronous ...
await Task.Yield(); await Task.Yield();
using var cts = new CancellationTokenSource(); using var cts = new CancellationTokenSource();
logger.LogInformation($"RabbitMQ Cosume Topic: {topic} ...");
// //
// Subscribe to Topic ... // Subscribe to Topic ...
@@ -103,9 +104,10 @@ namespace xEventService.Providers
); );
} }
} }
catch catch (Exception ex)
{ {
// //
logger.LogError($"RabbitMQ Cosume Error: {ex.Message} ...");
if (configuration.RabbitMQ.ManualAck) if (configuration.RabbitMQ.ManualAck)
{ {
// //
@@ -153,10 +155,20 @@ namespace xEventService.Providers
public override async Task StopAsync(CancellationToken cancellationToken) public override async Task StopAsync(CancellationToken cancellationToken)
{ {
// //
channel.Close(); if (channel != null)
channel.Dispose(); {
connection.Close(); //
connection.Dispose(); channel.Close();
channel.Dispose();
}
//
if (connection != null)
{
//
connection.Close();
connection.Dispose();
}
// //
await base.StopAsync(cancellationToken); await base.StopAsync(cancellationToken);
@@ -199,8 +211,9 @@ namespace xEventService.Providers
if (result.IsNullOrEmpty()) if (result.IsNullOrEmpty())
{ {
// //
var exchangeName = topic ?? configuration.RabbitMQ.Exchange;
var digits = Guid.NewGuid().ToString().GetDigits(); var digits = Guid.NewGuid().ToString().GetDigits();
result = $"{configuration.RabbitMQ.Exchange}[{digits}]"; result = $"{exchangeName}[{digits}]";
} }
// //
@@ -228,12 +241,12 @@ namespace xEventService.Providers
channel = connection.CreateModel(); channel = connection.CreateModel();
} }
private void DeclareTopology() private void DeclareTopology(string topic)
{ {
// //
channel.ExchangeDeclare( channel.ExchangeDeclare(
arguments: null, arguments: null,
exchange: configuration.RabbitMQ.Exchange, exchange: topic ?? configuration.RabbitMQ.Exchange,
durable: configuration.RabbitMQ.ExchangeDurable, durable: configuration.RabbitMQ.ExchangeDurable,
autoDelete: configuration.RabbitMQ.ExchangeAutoDelete, autoDelete: configuration.RabbitMQ.ExchangeAutoDelete,
type: configuration.RabbitMQ.ExchangeType.GetStringValue() type: configuration.RabbitMQ.ExchangeType.GetStringValue()
@@ -255,7 +268,7 @@ namespace xEventService.Providers
channel.QueueBind( channel.QueueBind(
arguments: null, arguments: null,
queue: queueName, queue: queueName,
exchange: configuration.RabbitMQ.Exchange, exchange: topic ?? configuration.RabbitMQ.Exchange,
routingKey: configuration.RabbitMQ.RoutingKey routingKey: configuration.RabbitMQ.RoutingKey
); );
} }
+5 -3
View File
@@ -143,7 +143,7 @@ namespace xEventService.Providers
{ {
// //
// Ensure Connection is Alive ... // Ensure Connection is Alive ...
EnsureChannelAsync(); EnsureChannel();
// //
// Serialize Message ... // Serialize Message ...
@@ -181,8 +181,10 @@ namespace xEventService.Providers
// //
result = true; result = true;
} }
catch catch (Exception ex)
{ {
//
logger.LogError($"RabbitMQ Produce Error: {ex.Message} ...");
result = false; result = false;
} }
@@ -268,7 +270,7 @@ namespace xEventService.Providers
/// <summary> /// <summary>
/// Ensure Channel is Open and Exchange/Queue Declared ... /// Ensure Channel is Open and Exchange/Queue Declared ...
/// </summary> /// </summary>
private void EnsureChannelAsync() private void EnsureChannel()
{ {
// //
if (IsConnected()) if (IsConnected())
+22 -3
View File
@@ -40,7 +40,8 @@ namespace xEventService.Providers
{ {
// //
// Validating Configuration ... // Validating Configuration ...
if (configuration.Broker != XEventBroker.XKafka) if (!configuration.ZeroMQ.IsValid() ||
configuration.Broker != XEventBroker.XZeroMQ)
{ {
XException.InvalidConfiguration.Throw(); XException.InvalidConfiguration.Throw();
} }
@@ -61,6 +62,7 @@ namespace xEventService.Providers
{ {
// //
await Task.Yield(); await Task.Yield();
logger.LogInformation($"ZeroMQ Cosume Topic: {topic} ...");
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
// //
@@ -70,6 +72,7 @@ namespace xEventService.Providers
string receivedTopic = string.Empty; string receivedTopic = string.Empty;
byte[] body = null; byte[] body = null;
//
if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub) if (configuration.ZeroMQ.Pattern == XZeroMQPattern.PubSub)
{ {
// //
@@ -80,6 +83,10 @@ namespace xEventService.Providers
{ {
// Process // Process
} }
else
{
logger.LogError($"ZeroMQ Cosume Error ...");
}
} }
} }
else else
@@ -88,8 +95,13 @@ namespace xEventService.Providers
{ {
// Process // Process
} }
else
{
logger.LogError($"ZeroMQ Cosume Error ...");
}
} }
//
if (body != null && body.Length > 0) if (body != null && body.Length > 0)
{ {
// //
@@ -113,8 +125,10 @@ namespace xEventService.Providers
await Task.Delay(10, cancellationToken); await Task.Delay(10, cancellationToken);
} }
} }
catch (Exception) catch (Exception ex)
{ {
//
logger.LogError($"ZeroMQ Cosume Error: {ex.Message} ...");
await Task.Delay(1000, cancellationToken); await Task.Delay(1000, cancellationToken);
} }
} }
@@ -123,7 +137,12 @@ namespace xEventService.Providers
public override async Task StopAsync(CancellationToken cancellationToken) public override async Task StopAsync(CancellationToken cancellationToken)
{ {
// //
socket.Close(); if (socket != null)
{
socket.Close();
}
//
await base.StopAsync(cancellationToken); await base.StopAsync(cancellationToken);
} }