private async Task <MessagingContext> HandleNotifyException(Exception exception, MessagingContext context)
        {
            Logger.Error(exception.Message);

            using (var dbContext = _createContext())
            {
                var repository = new DatastoreRepository(dbContext);

                if (context.NotifyMessage.EntityType == typeof(InMessage) ||
                    context.NotifyMessage.EntityType == typeof(InException))
                {
                    var inException = InException.ForEbmsMessageId(context.EbmsMessageId, exception);
                    repository.InsertInException(inException);

                    if (context.NotifyMessage.EntityType == typeof(InMessage))
                    {
                        Logger.Debug("Fatal fail in notification, set InMessage(s).Status=Exception");
                        repository.UpdateInMessage(context.EbmsMessageId, i => i.SetStatus(InStatus.Exception));
                    }
                }
                else if (context.NotifyMessage.EntityType != typeof(OutMessage) ||
                         context.NotifyMessage.EntityType == typeof(OutException))
                {
                    var outException = OutException.ForEbmsMessageId(context.EbmsMessageId, exception);
                    repository.InsertOutException(outException);

                    if (context.NotifyMessage.EntityType == typeof(OutMessage) &&
                        context.MessageEntityId != null)
                    {
                        Logger.Debug("Fatal fail in notification, set OutMessage.Status=Exception");
                        repository.UpdateOutMessage(
                            context.MessageEntityId.Value,
                            o => o.SetStatus(OutStatus.Exception));
                    }
                }

                if (context.MessageEntityId != null)
                {
                    Logger.Debug("Abort retry operation due to fatal notification exception, set Status=Completed");
                    repository.UpdateRetryReliability(
                        context.MessageEntityId.Value,
                        r => r.Status = RetryStatus.Completed);
                }

                await dbContext.SaveChangesAsync();
            }

            return(new MessagingContext(exception));
        }