SDL Tridion EventSubscription UnSubscribe问题

时间:2012-11-22 21:50:10

标签: c# .net tridion unsubscribe

我们正试图取消订阅我们的赛事代码中的eventSubscripton。我们使用以下代码

[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();
    }}

我们观察到的是,一旦调用了“{EventSubsciption} .Unsubscribe”,事件就会停止为它应该工作的后续事件工作。一旦重新启动与事件系统相关的服务,事件代码将首次按预期工作,并且永远不会为后续事件(它应该为其工作)调用。

1 个答案:

答案 0 :(得分:1)

尝试删除Dispose方法,看看是否有所作为。 Tridion可能会在触发事件的第一个实例时实例化事件处理程序,然后在系统重新启动之前再也不会再执行它。因此,如果您取消订阅并进行处置,那么您的课程将不会再次实例化。您的环境中的其他内容也可能会干扰。很难说,但首先要删除Dispose。

我的样板处理程序看起来像这样:

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!
        }

    }
}