Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDL Tridion EventSubscription UnSubscribe Issue

We are trying to Unsubscribe from the eventSubscripton in our Eventing Code. We are using the following code

[TcmExtension("EventHandlerExtension")]
public class EventHandler : TcmExtension, IDisposable
{
    private EventSubscription componentSaveSubscription = null;
    private EventSubscription componentPublishSubscription = null;

    #region Public Methods
    /// <summary>
    /// Handle for Eventing System
    /// </summary>
    public EventHandler()
    {
        Subscribe();
    }
    /// <summary>
    /// Subscribe Events
    /// </summary>
    public void Subscribe()
    {
        //News Article Page created when component Save
        componentSaveSubscription = EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePost, EventPhases.TransactionCommitted);

        //EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePost, EventPhases.TransactionCommitted);
        componentPublishSubscription = EventSystem.Subscribe<Component, PublishOrUnPublishEventArgs>(OnComponentPublishOrUnPublishPost, EventPhases.TransactionCommitted);
        //EventSystem.Subscribe<StructureGroup, PublishOrUnPublishEventArgs>(OnStructureGroupPublishInitiated, EventPhases.TransactionCommitted);

    }
    /// <summary>
    /// IDisposable Implementation
    /// </summary>
    public void Dispose()
    {

        if (componentSaveSubscription != null) componentSaveSubscription.Unsubscribe();
        if (componentPublishSubscription != null) componentPublishSubscription.Unsubscribe();
    }}

What we have observed is that once “{EventSubsciption}.Unsubscribe” is called, eventing stops working for the subsequent events for which it is supposed to work. Once the event system related services are restarted, the eventing code works as expected for the first time only and never gets called for subsequent events(for which it was supposed to work).

like image 251
Paras Avatar asked Nov 12 '22 17:11

Paras


1 Answers

Try removing the Dispose method and see if that makes a difference. It's possible that Tridion instantiates the Event Handler upon the first instance of an event being fired, and then never does it again until the system is restarted. So if you unsubscribe and dispose, then your class won't be instantiated again. It's also possible that something else in your environment is interfering. Hard to say, but try by removing the Dispose first.

My boilerplate Handler looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace NicksEventSystem
{
    [TcmExtension("NicksEventSystemExtension")]
    public class NicksEventHandler : TcmExtension
    {
        public NicksEventHandler()
        {
            Subscribe();
        }

        private void Subscribe()
        {
            EventSystem.Subscribe<Component, FinishActivityEventArgs>(MyEvent, EventPhases.TransactionCommitted);
        }

        private void MyEvent(Component wfComponent)
        {
            //... Do stuff!
        }

    }
}
like image 137
Nickoli Roussakov Avatar answered Nov 15 '22 05:11

Nickoli Roussakov