Tridion - 事件系统对象未被破坏

And*_*ler 6 tridion tridion-2011

我创建了一个TcmExtension命名WorkflowEventSystem,其中有一个事件处理程序订阅了该FinishProcess事件.此事件的目的是安排发布相关工作流主题的所有依赖项(即页面).

我遇到的问题是,即使事件在正确的时间触发(工作流程完成),以及应该安排发布的所有项目,PublishScheduler事件创建的对象似乎永远不会消失范围,因此WorkflowEventSystem对象也没有.

关于事件系统如何工作会导致这些对象永远存在,我是否缺少一些东西?我已将我认为的相关代码包括在内(部分内容汇总).谢谢你的帮助.

这是大多数实际的TcmExtension:

public class WorkflowEventSystem : TcmExtension
{
    public WorkflowEventSystem()
    {
        this.Subscribe();
    }

    public void Subscribe()
    {
        EventSystem.Subscribe<ProcessInstance, FinishProcessEventArgs>(ScheduleForPublish, EventPhases.All);
    }
}
Run Code Online (Sandbox Code Playgroud)

ScheduleForPublish创建一个PublishScheduler对象(我创建的类):

private void ScheduleForPublish(ProcessInstance process, FinishProcessEventArgs e, EventPhases phase)
{
    if(phase == EventPhases.TransactionCommitted)
    {
        PublishScheduler publishScheduler = new PublishScheduler(process);
        publishScheduler.ScheduleForPublish(process);
        publishScheduler = null;  // worth a try
    }
}
Run Code Online (Sandbox Code Playgroud)

ScheduleForPublish方法看起来类似于:

public void ScheduleForPublish(ProcessInstance process)
{
    using(Session session = new Session("ImpersonationUser"))
    {
        var publishInstruction = new PublishInstruction(session);
        // Set up some publish Instructions

       var publicationTargets = new List<PublicationTarget>();
       // Add a PublicationTarget here by tcm id

       IList<VersionedItem> itemsToPublish = new List<VersionedItem>();
       // Add the items we want to publish by calling GetUsingItems(filter)
       // on the workflow process' subject

       //Publish the items
       PublishEngine.Publish(itemsToPublish.Cast<IdentifiableObject>(), publishInstruction, publishTargets);
    }    
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 10

类的生命周期管理TcmExtension非常简单:

  1. 当您调用指定SubscribeTcmExtension对象时,会将其添加到内部订阅列表中

  2. 当您稍后调用Unsubscribe相同的TcmExtension对象时,将从订阅列表中删除

既然你永远不会打电话,UnsubscribeWorkflowEventSystem永远不会被删除,因此.NET永远不会被垃圾收集.并且由于您WorkflowEventSystem拥有PublishScheduler对其创建的实例的引用,因此也将永远不会清理该实例.

适用于自定义的样板TcmExtension是:

public class WorkflowEventSystem : TcmExtension, IDisposable
{
    EventSubscription _subscription;

    public WorkflowEventSystem()
    {
        this.Subscribe();
    }

    public void Subscribe()
    {
         _subscription = EventSystem.Subscribe<ProcessInstance, 
             FinishProcessEventArgs>(ScheduleForPublish, EventPhases.All);
    }

    public void Dispose()
    {
        _subscription.Unsubscribe();
    }
}
Run Code Online (Sandbox Code Playgroud)

Nuno在本文中给出了一个更长的例子(处理多个订阅者):http: //nunolinhares.blogspot.nl/2012/07/validating-content-on-save-part-1-of.html