public async Task SendMessage <T>(string queueUrl, object message, bool isFifo) { try { var sendMessageRequest = new SendMessageRequest { QueueUrl = queueUrl, MessageBody = JsonSerializer.Serialize(message), MessageAttributes = SqsMessageTypeAttributehelper.CreateAttributes <T>() }; if (isFifo) { sendMessageRequest.MessageGroupId = typeof(T).Name; sendMessageRequest.MessageDeduplicationId = Guid.NewGuid().ToString(); } await AmazonSQSClient.SendMessageAsync(sendMessageRequest).ConfigureAwait(false); } catch (Exception ex) { Devon4NetLogger.Error($"Failed to PostMessagesAsync for queue {queueUrl}: {ex.Message}"); Devon4NetLogger.Error(ex); throw; } }
public async Task <List <Message> > GetSqsMessages(string queueUrl, int maxNumberOfMessagesToRetrievePerCall = 1, int ReceiveMessageWaitTimeSeconds = 0, CancellationToken cancellationToken = default) { try { var result = await AmazonSQSClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = queueUrl, MaxNumberOfMessages = maxNumberOfMessagesToRetrievePerCall, WaitTimeSeconds = ReceiveMessageWaitTimeSeconds, AttributeNames = new List <string> { QueueAttributeName.All }, MessageAttributeNames = new List <string> { QueueAttributeName.All } }, cancellationToken).ConfigureAwait(false); return(result?.Messages); } catch (Exception ex) { Devon4NetLogger.Error($"Failed to GetSqsMessages for queue {queueUrl}: {ex.Message}"); Devon4NetLogger.Error(ex); throw; } }
public async Task DeleteSqsQueue(string queueUrl, bool waitForDeletion = false) { try { await AmazonSQSClient.DeleteQueueAsync(queueUrl).ConfigureAwait(false); if (!waitForDeletion) { return; } var queueExists = true; do { var queues = await GetSqsQueues().ConfigureAwait(false); queueExists = queues.Any(q => q == queueUrl); await Task.Delay(1000); } while (queueExists); } catch (Exception ex) { Devon4NetLogger.Error($"Error deleting the queue: {queueUrl}"); Devon4NetLogger.Error(ex); } }
public async Task <SqsQueueStatus> GetQueueStatus(string queueName) { CheckQueueName(queueName); var queueUrl = await GetQueueUrl(queueName).ConfigureAwait(false); try { var attributes = new List <string> { QueueAttributeName.ApproximateNumberOfMessages, QueueAttributeName.ApproximateNumberOfMessagesNotVisible, QueueAttributeName.LastModifiedTimestamp, QueueAttributeName.ApproximateNumberOfMessagesDelayed }; var response = await AmazonSQSClient.GetQueueAttributesAsync(new GetQueueAttributesRequest(queueUrl, attributes)).ConfigureAwait(false); return(new SqsQueueStatus { IsHealthy = response.HttpStatusCode == HttpStatusCode.OK, QueueName = queueName, ApproximateNumberOfMessages = response.ApproximateNumberOfMessages, ApproximateNumberOfMessagesNotVisible = response.ApproximateNumberOfMessagesNotVisible, LastModifiedTimestamp = response.LastModifiedTimestamp, ApproximateNumberOfMessagesDelayed = response.ApproximateNumberOfMessagesDelayed, IsFifo = response.FifoQueue }); } catch (Exception ex) { Devon4NetLogger.Error($"Failed to GetNumberOfMessages for queue {queueName}: {ex.Message}"); Devon4NetLogger.Error(ex); throw; } }
public BsonValue CreateMessageBackup(Command command, QueueActions action = QueueActions.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) { try { if (command?.InternalMessageIdentifier == null || command.InternalMessageIdentifier.IsNullOrEmptyGuid()) { throw new ArgumentException($"The provided command and the command identifier cannot be null "); } var backUp = new RabbitBackup { Id = Guid.NewGuid(), InternalMessageIdentifier = command.InternalMessageIdentifier, Retries = increaseRetryCounter ? 1 : 0, AdditionalData = string.IsNullOrEmpty(additionalData) ? string.Empty : additionalData, IsError = false, MessageContent = GetSerializedContent(command), MessageType = command.MessageType, TimeStampUTC = command.Timestamp.ToUniversalTime(), Action = action.ToString(), Error = string.IsNullOrEmpty(errorData) ? string.Empty : errorData }; var result = RabbitMqBackupLiteDbRepository.Create(backUp); return(result); } catch (Exception ex) { Devon4NetLogger.Error($"Error storing data with LiteDb: {ex.Message} {ex.InnerException}"); Devon4NetLogger.Error(ex); throw; } }
private Task HandleException(ref HttpContext context, ref System.Exception exception) { Devon4NetLogger.Error(exception); var exceptionTypeValue = exception.GetType(); List <string> exceptionInterfaces = exceptionTypeValue.GetInterfaces().Select(i => i.Name).ToList(); exceptionInterfaces.Add(exceptionTypeValue.Name); switch (exceptionInterfaces) { case List <string> exceptionType when exceptionType.Contains("InvalidDataException"): return(HandleContext(ref context, StatusCodes.Status422UnprocessableEntity)); case List <string> exceptionType when exceptionType.Contains("ArgumentException") || exceptionType.Contains("ArgumentNullException") || exceptionType.Contains("NotFoundException") || exceptionType.Contains("FileNotFoundException"): return(HandleContext(ref context, StatusCodes.Status400BadRequest)); case List <string> exceptionType when exceptionType.Contains("IWebApiException"): return(HandleContext(ref context, ((IWebApiException)exception).StatusCode, ((IWebApiException)exception).ShowMessage ? exception.Message : null)); default: return(HandleContext(ref context, StatusCodes.Status500InternalServerError)); } }
public async Task <DeliveryResult <T, TV> > DeliverMessage <T, TV>(T key, TV value, string producerId) where T : class where TV : class { DeliveryResult <T, TV> result; var producerOptions = GetProducerOptions(producerId); using var producer = GetProducerBuilder <T, TV>(producerId); try { result = await producer.ProduceAsync(producerOptions.Topic, new Message <T, TV> { Key = key, Value = value }).ConfigureAwait(false); } catch (ProduceException <string, string> e) { Devon4NetLogger.Error(e); throw; } finally { producer?.Flush(); producer?.Dispose(); } return(result); }
public async Task <MediatRBackup> CreateResponseMessageBackup(object command, MediatRActionsEnum action = MediatRActionsEnum.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) { try { var ctx = CreateContext(); var backUp = new MediatRBackup { Id = Guid.NewGuid(), Retries = increaseRetryCounter ? 1 : 0, AdditionalData = string.IsNullOrEmpty(additionalData) ? string.Empty : additionalData, IsError = false, MessageContent = GetSerializedContent(command), //System.Text.Json.JsonSerializer.Serialize(command), TimeStampUTC = DateTime.UtcNow.ToUniversalTime(), Action = action.ToString(), Error = string.IsNullOrEmpty(errorData) ? string.Empty : errorData }; var result = await ctx.MediatRBackup.AddAsync(backUp).ConfigureAwait(false); await ctx.SaveChangesAsync(); await ctx.DisposeAsync(); return(result.Entity); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public bool ValidateToken(string jwtToken, out ClaimsPrincipal claimsPrincipal, out SecurityToken securityToken) { var handler = new JwtSecurityTokenHandler(); try { claimsPrincipal = handler.ValidateToken(jwtToken, new TokenValidationParameters { ValidateIssuer = JwtOptions.ValidateIssuer, ValidateAudience = JwtOptions.RequireAudience, ValidateIssuerSigningKey = JwtOptions.ValidateIssuerSigningKey, ValidateLifetime = JwtOptions.ValidateLifetime, RequireSignedTokens = JwtOptions.RequireSignedTokens, RequireExpirationTime = JwtOptions.RequireExpirationTime, RequireAudience = JwtOptions.RequireAudience, TokenDecryptionKey = SecurityKey, IssuerSigningKey = SecurityKey, ValidAudience = JwtOptions.Audience, ValidIssuer = JwtOptions.Issuer }, out securityToken); return(true); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public BsonValue CreateMessageBackup <T>(ActionBase <T> command, MediatrActions action = MediatrActions.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) where T : class { try { if (command?.InternalMessageIdentifier == null || command.InternalMessageIdentifier.IsNullOrEmptyGuid()) { throw new ArgumentException($"The provided command and the command identifier cannot be null "); } var backUp = new MediatRBackup { Id = Guid.NewGuid(), InternalMessageIdentifier = command.InternalMessageIdentifier, Retries = increaseRetryCounter ? 1 : 0, AdditionalData = string.IsNullOrEmpty(additionalData) ? string.Empty : additionalData, IsError = false, MessageContent = GetSerializedContent(command), //System.Text.Json.JsonSerializer.Serialize(command), MessageType = command.MessageType, TimeStampUTC = command.Timestamp.ToUniversalTime(), Action = action.ToString(), Error = string.IsNullOrEmpty(errorData) ? string.Empty : errorData }; return(MediatRBackupLiteDbRepository.Create(backUp)); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public async Task <string> CreateSqsQueue(SqsQueueOptions sqsQueueOptions) { var queueAttrs = new Dictionary <string, string>(); try { queueAttrs.Add(QueueAttributeName.FifoQueue, sqsQueueOptions.UseFifo.ToString().ToLowerInvariant()); queueAttrs.Add(QueueAttributeName.DelaySeconds, sqsQueueOptions.DelaySeconds.ToString().ToLowerInvariant()); queueAttrs.Add(QueueAttributeName.MaximumMessageSize, sqsQueueOptions.MaximumMessageSize.ToString().ToLowerInvariant()); queueAttrs.Add(QueueAttributeName.ReceiveMessageWaitTimeSeconds, sqsQueueOptions.ReceiveMessageWaitTimeSeconds.ToString().ToLowerInvariant()); if (sqsQueueOptions.RedrivePolicy != null && !string.IsNullOrEmpty(sqsQueueOptions.RedrivePolicy.DeadLetterQueueUrl) && sqsQueueOptions.RedrivePolicy.MaxReceiveCount > 0) { queueAttrs.Add(QueueAttributeName.RedrivePolicy, await GetRedrivePolicy(sqsQueueOptions.RedrivePolicy).ConfigureAwait(false)); } var responseCreate = await AmazonSQSClient.CreateQueueAsync(new CreateQueueRequest { QueueName = sqsQueueOptions.QueueName, Attributes = queueAttrs }).ConfigureAwait(false); return(responseCreate.QueueUrl); } catch (QueueNameExistsException ex) { Devon4NetLogger.Error($"Error creating the queue: {sqsQueueOptions.QueueName}. The queue name already exists"); Devon4NetLogger.Error(ex); throw; } catch (QueueDeletedRecentlyException ex) { Devon4NetLogger.Error($"Error creating the queue: {sqsQueueOptions.QueueName}. The queue has been deleted recently"); Devon4NetLogger.Error(ex); throw; } }
private async Task <bool> SaveChanges() { try { await Context.SaveChangesAsync().ConfigureAwait(false); return(true); } catch (OperationCanceledException ex) { Devon4NetLogger.Error(ex); throw; } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { var databaseValues = entry.GetDatabaseValues(); // Refresh original values to bypass next concurrency check entry.OriginalValues.SetValues(databaseValues); } Devon4NetLogger.Error(ex); throw; } catch (DbUpdateException ex) { Devon4NetLogger.Error(ex); throw; } }
public Task Commit(IDbContextTransaction transaction) { if (transaction == null) { throw new TransactionNullException("Transaction cannot be null to perform transaction operations."); } var transactionSavePointName = DateTime.UtcNow.ToString("yyyyMMddHHmmssfff", CultureInfo.InvariantCulture); return(Task.Run(async() => { try { transaction.CreateSavepoint(transactionSavePointName); _ = await SaveChanges().ConfigureAwait(false); await transaction.CommitAsync().ConfigureAwait(false); } catch (DbUpdateConcurrencyException ex) { Devon4NetLogger.Error(ex); RollbackTransaction(transaction, transactionSavePointName); throw; } catch (Exception ex) { Devon4NetLogger.Error(ex); RollbackTransaction(transaction, transactionSavePointName); throw; } })); }
public BsonValue CreateResponseMessageBackup(object command, MediatrActions action = MediatrActions.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) { try { var backUp = new MediatRBackup { Id = Guid.NewGuid(), Retries = increaseRetryCounter ? 1 : 0, AdditionalData = string.IsNullOrEmpty(additionalData) ? string.Empty : additionalData, IsError = false, MessageContent = GetSerializedContent(command), //System.Text.Json.JsonSerializer.Serialize(command), TimeStampUTC = DateTime.UtcNow.ToUniversalTime(), Action = action.ToString(), Error = string.IsNullOrEmpty(errorData) ? string.Empty : errorData }; return(MediatRBackupLiteDbRepository.Create(backUp)); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
private static async Task Migrate <T>(IServiceCollection services) where T : DbContext { try { using var sp = services.BuildServiceProvider(); if (sp == null) { Devon4NetLogger.Error($"Unable to build the service provider, the migration {typeof(T).FullName} will not be launched"); } else { var context = sp.GetService(typeof(T)); if (context == null) { Devon4NetLogger.Error($"Unable to resolve {typeof(T).FullName} and the migration will not be launched"); } ((T)context)?.Database.Migrate(); await sp.DisposeAsync().ConfigureAwait(false); } } catch (Exception ex) { Devon4NetLogger.Fatal(ex); } }
private Task HandleException(ref HttpContext context, ref System.Exception exception) { Devon4NetLogger.Error(exception); var exceptionTypeValue = exception.GetType(); var exceptionInterfaces = exceptionTypeValue.GetInterfaces().Select(i => i.Name).ToList(); exceptionInterfaces.Add(exceptionTypeValue.Name); return(exceptionInterfaces switch { { } exceptionType when exceptionType.Contains("InvalidDataException") => HandleContext(ref context, StatusCodes.Status422UnprocessableEntity), { } exceptionType when exceptionType.Contains("ArgumentException") || exceptionType.Contains("ArgumentNullException") || exceptionType.Contains("NotFoundException") || exceptionType.Contains("FileNotFoundException") => HandleContext(ref context, StatusCodes.Status400BadRequest), { } exceptionType when exceptionType.Contains("IWebApiException") => HandleContext(ref context, ((IWebApiException)exception).StatusCode, exception.Message, ((IWebApiException)exception).ShowMessage), _ => HandleContext(ref context, StatusCodes.Status500InternalServerError, exception.Message) });
private static void CheckQueueName(string queueName) { if (string.IsNullOrEmpty(queueName)) { Devon4NetLogger.Error("Queue name can not be null or empty"); throw new ArgumentException("Queue name can not be null or empty"); } }
public async Task <MediatRBackup> CreateMessageBackup <T>(ActionBase <T> command, MediatRActionsEnum action = MediatRActionsEnum.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) where T : class { MediatRBackupContext ctx = null; try { ctx = CreateContext(); if (ctx == null) { throw new ArgumentException("The database provider is not supported to host threads"); } if (!UseExternalDatabase) { throw new ArgumentException("Please setup your MeadiatRBackupContext database context to use MediatRBackupService"); } if (command?.InternalMessageIdentifier == null || command.InternalMessageIdentifier.IsNullOrEmptyGuid()) { throw new ArgumentException($"The provided command and the command identifier cannot be null "); } var backUp = new MediatRBackup { Id = Guid.NewGuid(), InternalMessageIdentifier = command.InternalMessageIdentifier, Retries = increaseRetryCounter ? 1 : 0, AdditionalData = string.IsNullOrEmpty(additionalData) ? string.Empty : additionalData, IsError = false, MessageContent = GetSerializedContent(command), MessageType = command.MessageType, TimeStampUTC = command.Timestamp.ToUniversalTime(), Action = action.ToString(), Error = string.IsNullOrEmpty(errorData) ? string.Empty : errorData }; var result = await ctx.MediatRBackup.AddAsync(backUp).ConfigureAwait(false); await ctx.SaveChangesAsync(); await ctx.DisposeAsync(); return(result.Entity); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } finally { if (ctx != null) { await ctx.DisposeAsync().ConfigureAwait(false); } } }
private static ProducerBuilder <T, TV> GetProducerBuilderInstance <T, TV>(ProducerConfig configuration) where T : class where TV : class { var producer = new ProducerBuilder <T, TV>(configuration); producer.SetErrorHandler((_, e) => Devon4NetLogger.Error(new ConsumerException($"Error code {e.Code} : {e.Reason}"))); producer.SetStatisticsHandler((_, json) => Devon4NetLogger.Information($"Statistics: {json}")); producer.SetLogHandler((c, partitions) => { Devon4NetLogger.Information($"Kafka log handler: [{string.Join(", ", partitions)}]"); }); return(producer); }
private static Task HandleExceptionAsync(HttpContext context, Exception exception) { Devon4NetLogger.Error(exception); var result = JsonSerializer.Serialize(new { error = exception.Message }); context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(context.Response.WriteAsync(result)); }
public async Task <HttpResponseMessage> Send(HttpMethod httpMethod, string endPointName, string url, object content, string mediaType, bool contentAsJson = true, bool useCamelCase = false, Dictionary <string, string> headers = null) { try { return(await SendCommand(httpMethod, endPointName, url, content, mediaType, headers, contentAsJson, useCamelCase).ConfigureAwait(false)); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public override void OnResultExecuting(ResultExecutingContext context) { try { base.OnResultExecuting(context); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
private static void RollbackTransaction(IDbContextTransaction transaction, string savePoint) { if (transaction == null) { const string message = "Transaction cannot be null to perform transaction operations"; Devon4NetLogger.Error(message); throw new TransactionNullException(message); } transaction.RollbackToSavepoint(savePoint); transaction.Dispose(); }
private static X509Certificate2 LoadServerCertificate(string kestrelCertificate, string kestrelCertificatePassword) { try { return(new X509Certificate2(File.ReadAllBytes(FileOperations.GetFileFullPath(kestrelCertificate)), kestrelCertificatePassword, X509KeyStorageFlags.MachineKeySet)); } catch (Exception) { Devon4NetLogger.Error("Error loading the server certificate. Please check the certificate's path is not null and check the password is correct"); throw; } }
/// <summary> /// RabbitMq handle the messages with threads /// EF Context multi-thread is not allowed /// The solution to handle is creating and disposing a database context /// Please check https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#avoiding-dbcontext-threading-issues /// </summary> /// <returns>The database context to backup the messages</returns> private RabbitMqBackupContext CreateContext() { var errorMessage = "The connection string from context cannot be null or the database provider is not supported"; if (string.IsNullOrEmpty(ContextConnectionString)) { Devon4NetLogger.Error(errorMessage); } var optionsBuilder = new DbContextOptionsBuilder <RabbitMqBackupContext>(); switch (ContextProvider) { case DatabaseConst.SqlServer: optionsBuilder.UseSqlServer(ContextConnectionString); break; case DatabaseConst.PostgresSql: optionsBuilder.UseNpgsql(ContextConnectionString); break; case DatabaseConst.MySql: case DatabaseConst.MySqlPomelo: optionsBuilder.UseMySql(ContextConnectionString); break; case DatabaseConst.FireBirdSql: case DatabaseConst.FireBirdSqlV: optionsBuilder.UseFirebird(ContextConnectionString); break; case DatabaseConst.SqlLite: optionsBuilder.UseSqlite(ContextConnectionString); break; //Oracle does not support EF Core 3.1 yet //case DatabaseConst.Oracle: // optionsBuilder.UseOracle(ContextConnectionString,sqlOptions => { }); // break; //IBM does not support EF Core 3.1 yet //case DatabaseConst.Ibm: // optionsBuilder.UseDb2(ContextConnectionString, sqlOptions => { }); // break; default: Devon4NetLogger.Error(errorMessage); throw new ArgumentException(errorMessage); } return(new RabbitMqBackupContext(optionsBuilder.Options)); }
private static string GetEncryptionAlgorithm(string encryptionAlgorithm) { try { var securityAlgorithm = StaticConstsHelper.GetValue(typeof(SecurityAlgorithms), string.IsNullOrWhiteSpace(encryptionAlgorithm) ? AuthConst.DefaultAlgorithm : encryptionAlgorithm); return(securityAlgorithm ?? SecurityAlgorithms.RsaSha512); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public async Task <HelloReply> Get(string name) { try { var client = new Greeter.GreeterClient(GrpcChannel); return(await client.SayHelloAsync(new HelloRequest { Name = name }).ResponseAsync.ConfigureAwait(false)); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
private void GetContextConnectionAndProvider(RabbitMqBackupContext context) { try { ContextProvider = context.Database.ProviderName; ContextConnectionString = context.Database.GetDbConnection().ConnectionString; } catch (Exception ex) { Devon4NetLogger.Error("Error trying to get the connection string from context."); Devon4NetLogger.Error(ex); } }
protected CustomFluentValidator(bool launchExceptionWhenError = false) { LaunchExceptionWhenError = launchExceptionWhenError; try { CallCustomValidationImplementation(); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }
public override void OnActionExecuting(ActionExecutingContext context) { try { var controllerValues = GetControllerProperties((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor, context.ActionArguments); LogEvent("OnActionExecuting", $"Controller: {controllerValues.ControllerName} | method: {controllerValues.ControllerMethod}| ActionArguments: {controllerValues.ActionArguments}"); base.OnActionExecuting(context); } catch (Exception ex) { Devon4NetLogger.Error(ex); throw; } }