Event Handling (Using new table)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi,

I have a new table called "subscribe" and I create a new plugin to insert data into this table.
Then I have an another plugin to sync this subscribe user to mailchimp.

I want to raise an event when a new subscriber inserted into "Subscribe" table and this event needs to be handled into the second plugin i.e. mailchimp plugin to sync this subscriber to mailchimp.

I wire up all the necessary configuration and event is raised by the first plugin but when control goes to Publish method of class "EventPublisher" under "Nop.Services.Events" it returns empty array from scope.

Please help me out.
6 years ago
Can you share some code of what you have tried so far?
6 years ago
Hi,

Here is code :

In my first plugin :

public int Subscribehoroscope(SubscribeHoroscopeModel subscribeHoroscope)
        {
            try
            {
                Horoscopes.Domain.SubscribeHoroscope horo = (from h in _subscribesRepository.Table
                                                             where h.Email == subscribeHoroscope.Email
                                                             select h).FirstOrDefault();

                if (horo == null)
                {

                    Guid guid = Guid.NewGuid();

                    horo = new SubscribeHoroscope();
                    horo.Active = subscribeHoroscope.Active;
                    horo.CreatedOnUtc = subscribeHoroscope.CreatedOnUtc;
                    horo.SunSignId = subscribeHoroscope.SunSignId;
                    horo.Email = subscribeHoroscope.Email;
                    horo.HoroscopeSubscriptionGuid = guid;
                    horo.Name = subscribeHoroscope.Name;
                    horo.StoreId = subscribeHoroscope.StoreId;
                    horo.CustomerId = subscribeHoroscope.CustomerId;
                    horo.IsSent = false;

                    _subscribesRepository.Insert(horo);

                    //event notification
                    _eventPublisher.EntityInserted(horo);

                    return horo.Id;
                }
                else
                {
                    return -1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



And in my second plugin :

public class MailChimpECommerceEventConsumer : IConsumer<EntityInserted<SubscribeHoroscope>>,
        IConsumer<EntityUpdated<SubscribeHoroscope>>

{
/// <summary>
        /// Handles the event.
        /// </summary>
        /// <param name="insertedOrder">Inserted order</param>
        public void HandleEvent(EntityInserted<SubscribeHoroscope> inserted)
        {
            if (PluginIsActive)
            {
                _synchronizationRecordService.CreateOrUpdateRecord(EntityType.HoroscopeSubscription, inserted.Entity.Id, ActionType.Create, inserted.Entity.Email);
            }
        }

        /// <summary>
        /// Handles the event.
        /// </summary>
        /// <param name="insertedOrder">Inserted order</param>
        public void HandleEvent(EntityUpdated<SubscribeHoroscope> updated)
        {
            if (PluginIsActive)
            {
                _synchronizationRecordService.CreateOrUpdateRecord(EntityType.HoroscopeSubscription, updated.Entity.Id, ActionType.Update, updated.Entity.Email);
            }
        }
}


I'm not able to identify what I'm missing. When event publish from first plugin, scope returns empty array.


/// <summary>
        /// Publish event
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="eventMessage">Event message</param>
        public virtual void Publish<T>(T eventMessage)
        {
            var subscriptions = _subscriptionService.GetSubscriptions<T>();
            subscriptions.ToList().ForEach(x => PublishToConsumer(x, eventMessage));
        }
6 years ago
To me that code looks absolutely correct. It must be something else that is making it not work.

Could you try two things to perhaps isolate the problem:

1. In your second plugin: create a new EventConsumer for IConsumer<EntityInserted<Setting>>. Insert a new Setting and see if that is returning the subscriber in Publish<T>(T eventMessage).

If that is working:

2. Move your EventConsumer to a plugin where you know any subscription work, like NivoSlider plugin and see if it doesn't work there as well.
6 years ago
Thanks for your reply.

I tried both the scenario. In my first plugin there are 8-10 other EventConsumers already define and they works perfectly as they are using core tables of NOPCommerce like (Store,Product,Customer etc). But when I'm going to use that EventConsumer with my newly created table and context it doesn't work.
6 years ago
Very odd. I pasted your code in one of my projects and it worked fine (unfortunately). Are you sure SubscribeHoroscope is the exact same type in both the publisher and the subscriber?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.