2009-06-11 97 views
2

我有一個工作流程,可以監視某些數據庫,並在注意到觸發器時啓動其他工作流程。我只希望這個「觀察員」工作流程的一個實例能夠在任何時間點進行;否則,如果有兩個或兩個以上的人在運行,他們都會注意到這個變化,並且都會觸發相同的工作流程,這樣做效果不佳。Windows工作流程:「singleton」式工作流程?

這個「觀察者」工作流程持續存在。所以......我該如何做到這一點,以便如果運行時沒有這個工作流的實例已經存在,它會啓動一個,但是如果一個已經存在,那就使用持久化的呢?

幾乎聽起來像我可以創建一個小型的一次性控制檯應用程序,它啓動了我想要的工作流程,然後「真正的」運行時只是拉動了持久的一個,並且從不試圖創建一個新的,這聽起來很優雅。

回答

1

前段時間我們在一個項目中遇到了這個問題。我們提出的解決方案是主辦兩個運行時間;一個具有持久性服務,另一個沒有。在沒有持久性服務的運行時,我們運行了幾種這種「監視工作流程」,這些工作流程在主機啓動時自動啓動。

這是它是如何實現的:

首先,我們必須在我們成立了持久性服務的配置文件:

<configuration> 
    <configSections> 
     <section name="RuntimeWithPersistence" type="System.Workflow.Runtime.Configuration.WorkflowRuntimeSection, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
    </configSections> 
    <RuntimeWithPersistence> 
     <CommonParameters/> 
     <Services> 
      <add type="System.Workflow.Runtime.Hosting.DefaultWorkflowSchedulerService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 
      <add type="System.Workflow.Runtime.Hosting.SqlWorkflowPersistenceService, System.Workflow.Runtime, Version=3.0.00000.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionString="[dbconnectionstring]" UnloadOnIdle="true"/> 
     </Services> 
    </RuntimeWithPersistence> 
</configuration>  

而且在工作流宿主應用程序(剪切和編輯,但我想我傳達的想法):

public class WorkflowHost 
{ 
    private WorkflowRuntime _runtime = null; 
    private WorkflowRuntime _nonPersistingRuntime = null; 

    private void SetupRuntime() 
    { 
     // create a new WorkflowRuntime that points out a config section 
     // defining a persistence service 
     _runtime = new WorkflowRuntime("RuntimeWithPersistence"); 
     // set up additional services to use 
     _runtime.StartRuntime() 

     // create a new WorkflowRuntime that does not point out a config section 
     _nonPersistingRuntime = new WorkflowRuntime(); 
     // set up additional services to use 
     _nonPersistingRuntime.StartRuntime() 
     // start monitoring workflows in the non persisting runtime 
     StartMonitoringWorkflows(_nonPersistingRuntime); 
    } 
} 
+0

感謝您的反饋!我也開始採用這種方法,但我試圖找到一個優雅的解決方案來解決另一個問題:監控人員必須知道哪些「觸發」它是之前被解僱的,因此下一次不會再這樣做它看到它。這就是爲什麼我在考慮在監視器中使用持久性的原因,因此它可以存儲一個觸發器列表,以忽略它已經執行的操作。我喜歡這個主意,並會更徹底地進行調查。 – Chris 2009-06-11 16:01:27

2

我正在考慮這個問題以及我目前正在工作的項目。但在我看來,監控數據庫的功能並不是工作流程的責任。

我們將創建一個服務以添加到運行時。該服務將引發工作流在HandleEventActivity中偵聽的事件。這樣工作流程就會閒置,持續下去並保持這種狀態直到真正需要做的工作。