protected virtual async Task TryDispatchHandlerPostCommitAsync(DomainEventHandlerMessage domainEventHandlerMessage)
 {
     try
     {
         await HandlePostCommitAsync(domainEventHandlerMessage).ConfigureAwait(false);
     }
     catch
     {
         //Log InProcess Post commit event failed
     }
 }
        protected override Task TryDispatchHandlerPostCommitAsync(DomainEventHandlerMessage domainEventHandlerMessage)
        {
            try
            {
                //Each Post Commit Domain Event Handling is completely independent. By registering the event AND handler (rather than just the event) in hangfire we get the granularity of retrying at a event/handler level.
                //Hangfire unfortunately uses System.Type.GetType to get job type. This only looks at the referenced assemblies of the web project and not the dynamic loaded plugins so need to
                //proxy back through this common assembly.

                var job = Job.FromExpression <IDomainEventsMediator>(m => m.HandlePostCommitAsync(domainEventHandlerMessage));

                var queue = new EnqueuedState(_serverSettings.ServerName);
                _backgroundJobClient.Create(job, queue);
            }
            catch
            {
                //Log Hangfire Post commit event Background enqueue failed
            }

            return(Task.CompletedTask);
        }
        //Event Dispatcher
        public async Task HandlePostCommitDispatchAsync(DomainEventMessage domainEventMessage)
        {
            var handlerTypes = _domainEventSubscriptionsManager.GetHandlersForEvent(domainEventMessage.DomainEvent);

            if (DispatchPostCommitEventsInParellel)
            {
                await Task.Run(() => Parallel.ForEach(handlerTypes, async handlerType =>
                {
                    var domainEventHandlerMessage = new DomainEventHandlerMessage(handlerType.HandlerType.FullName, domainEventMessage);
                    await TryDispatchHandlerPostCommitAsync(domainEventHandlerMessage).ConfigureAwait(false);
                }));
            }
            else
            {
                foreach (var handlerType in handlerTypes)
                {
                    var domainEventHandlerMessage = new DomainEventHandlerMessage(handlerType.HandlerType.FullName, domainEventMessage);
                    await TryDispatchHandlerPostCommitAsync(domainEventHandlerMessage).ConfigureAwait(false);
                }
            }
        }
        public async Task HandlePostCommitAsync(DomainEventHandlerMessage domainEventHandlerMessage)
        {
            Type handlerType = System.Type.GetType(domainEventHandlerMessage.HandlerType);

            if (handlerType == null)
            {
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    handlerType = assembly.GetType(domainEventHandlerMessage.HandlerType);
                    if (handlerType != null)
                    {
                        break;
                    }
                }
            }

            if (handlerType == null)
            {
                throw new Exception("Invalid handler type");
            }

            await DispatchPostCommitAsync(handlerType, domainEventHandlerMessage.DomainEventMessage.DomainEvent).ConfigureAwait(false);
        }