public async Task <IActionResult> SalvarPerfil([FromBody] PerfilItem perfilToSave)
        {
            //AREA DE VALIDACAO
            string msgRule = "";

            if (!ruleValidaNomePerfil(perfilToSave.nome_Perfil, ref msgRule))
            {
                return(BadRequest(msgRule));
            }

            //FIM AREA DE VALIDACAO

            if (_usuarioContext.Set <PerfilItem>().Any(e => e.id_Perfil == perfilToSave.id_Perfil))
            {
                _usuarioContext.PerfilItems.Update(perfilToSave);
            }
            else
            {
                _usuarioContext.PerfilItems.Add(perfilToSave);
            }

            //Create Integration Event to be published through the Event Bus
            var perfilSaveEvent = new PerfilSaveIE(perfilToSave.id_Perfil, perfilToSave.nome_Perfil);

            try
            {
                // Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
                await _usuarioIntegrationEventService.SaveEventAndPerfilContextChangesAsync(perfilSaveEvent, perfilToSave);
            }
            catch (Exception e)
            {
                //Validações das CONSTRAINTS do BANCO
                if (ruleValidaNomePerfilUnique(e.Message, ref msgRule))
                {
                    return(BadRequest(msgRule));
                }
                else
                {
                    return(BadRequest(e.Message));
                }
            }
            // Publish through the Event Bus and mark the saved event as published
            await _usuarioIntegrationEventService.PublishThroughEventBusAsync(perfilSaveEvent);


            return(CreatedAtAction(nameof(SalvarPerfil), perfilToSave.id_Perfil));
        }
 public async Task SaveEventAndPerfilContextChangesAsync(IntegrationEvent evt, PerfilItem perfilToSave)
 {
     var strategy = _usuarioContext.Database.CreateExecutionStrategy();
     await strategy.ExecuteAsync(async() =>
     {
         using (var transaction = _usuarioContext.Database.BeginTransaction())
         {
             try
             {
                 await _usuarioContext.SaveChangesAsync();
                 //Tratamento de Identity
                 ((Events.PerfilSaveIE)evt).PerfilId = perfilToSave.id_Perfil;
                 //Tratamento relacionamento
                 ((Events.PerfilSaveIE)evt).EmpresaPerfilItems = perfilToSave.EmpresaPerfilItems;
                 await _eventLogService.SaveEventAsync(evt, _usuarioContext.Database.CurrentTransaction.GetDbTransaction());
                 transaction.Commit();
             }
             catch (Exception ex)
             {
                 transaction.Rollback();
                 var sqlException = ex.InnerException as System.Data.SqlClient.SqlException;
                 throw new Exception(sqlException.Number + "::" + sqlException.Message);
             }
         }
     });
 }