Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

EventPublisher.cs

using Nop.Core.Events;
using Nop.Core.Infrastructure;
using Nop.Services.Logging;

namespace Nop.Services.Events;

/// 
/// Represents the event publisher implementation
/// 
public partial class EventPublisher : IEventPublisher
{
    #region Methods

    /// 
    /// Publish event to consumers
    /// 
    /// Type of event
    /// Event object
    /// A task that represents the asynchronous operation
    public virtual async Task PublishAsync(TEvent @event)
    {
        //get all event consumers
        var consumers = EngineContext.Current.ResolveAll>().ToList();

        foreach (var consumer in consumers)
        {
            try
            {
                //try to handle published event
                await consumer.HandleEventAsync(@event);
            }
            catch (Exception exception)
            {
                //log error, we put in to nested try-catch to prevent possible cyclic (if some error occurs)
                try
                {
                    var logger = EngineContext.Current.Resolve();
                    if (logger == null)
                        return;

                    await logger.ErrorAsync(exception.Message, exception);
                }
                catch
                {
                    // ignored
                }
            }
        }
    }

    /// 
    /// Publish event to consumers
    /// 
    /// Type of event
    /// Event object
    public virtual void Publish(TEvent @event)
    {
        //get all event consumers
        var consumers = EngineContext.Current.ResolveAll>().ToList();

        foreach (var consumer in consumers)
            try
            {
                //try to handle published event
                consumer.HandleEventAsync(@event).Wait();
            }
            catch (Exception exception)
            {
                //log error, we put in to nested try-catch to prevent possible cyclic (if some error occurs)
                try
                {
                    var logger = EngineContext.Current.Resolve();
                    if (logger == null)
                        return;

                    logger.Error(exception.Message, exception);
                }
                catch
                {
                    // ignored
                }
            }
    }

    #endregion
}