6

我创建了一个具有订阅事件的事件处理程序的TcmExtension名称。此事件的目的是安排发布相关工作流主题的所有依赖项(即页面)。WorkflowEventSystemFinishProcess

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

关于事件系统的工作原理,我是否遗漏了一些东西会导致这些对象永远存在?我在下面包含了我认为相关的代码(总结了一些部分)。谢谢你的帮助。

这是大部分实际的 TcmExtension:

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

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

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

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);
    }    
}
4

1 回答 1

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

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

于 2012-08-28T04:26:58.967 回答