Пример #1
0
        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);
                }
            }
        }
Пример #2
0
 private void GetContextConnectionAndProvider(MediatRBackupContext 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);
     }
 }
Пример #3
0
        private void CheckCommandContext <T>(ActionBase <T> command, MediatRBackupContext ctx) where T : class
        {
            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 ");
            }
        }
Пример #4
0
        public async Task <MediatRBackup> CreateMessageBackup <T>(ActionBase <T> command, MediatrActions action = MediatrActions.Sent, bool increaseRetryCounter = false, string additionalData = null, string errorData = null) where T : class
        {
            MediatRBackupContext ctx = null;

            try
            {
                ctx = CreateContext();

                CheckCommandContext(command, ctx);

                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().ConfigureAwait(false);

                await ctx.DisposeAsync().ConfigureAwait(false);

                return(result.Entity);
            }
            catch (Exception ex)
            {
                Devon4NetLogger.Error(ex);
                throw;
            }
            finally
            {
                if (ctx != null)
                {
                    await ctx.DisposeAsync().ConfigureAwait(false);
                }
            }
        }
Пример #5
0
 public MediatRBackupService(MediatRBackupContext context, IJsonHelper jsonHelper)
 {
     GetContextConnectionAndProvider(context);
     UseExternalDatabase = context != null;
     JsonHelper          = jsonHelper;
 }