主要用到了 SqlWorkflowInstanceStore

SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString);  
InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));  
instanceStore.DefaultInstanceOwner = view.InstanceOwner;

触发持久化数据库

application.Unload();  
    
或者              
    
            application.PersistableIdle = (e) =>  
            {  
                return PersistableIdleAction.Unload;  
            };

 全部代码

 

流程

<Activity mc:Ignorable="sap sap2010 sads" x:Class="WFTest.WorkflowConsole.Activity1"
 xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
 xmlns:local="clr-namespace:WFTest.WorkflowConsole"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:sads="http://schemas.microsoft.com/netfx/2010/xaml/activities/debugger"
 xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation"
 xmlns:sap2010="http://schemas.microsoft.com/netfx/2010/xaml/activities/presentation"
 xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"
 xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
  <x:Members> 
    <x:Property Name="argument1" Type="OutArgument(x:String)" /> 
  </x:Members> 
  <sap2010:ExpressionActivityEditor.ExpressionActivityEditor>C#</sap2010:ExpressionActivityEditor.ExpressionActivityEditor> 
  <sap2010:WorkflowViewState.IdRef>WFTest.WorkflowConsole.Activity1_1</sap2010:WorkflowViewState.IdRef> 
  <TextExpression.NamespacesForImplementation> 
    <sco:Collection x:TypeArguments="x:String"> 
      <x:String>System</x:String> 
      <x:String>System.Collections.Generic</x:String> 
      <x:String>System.Data</x:String> 
      <x:String>System.Linq</x:String> 
      <x:String>System.Text</x:String> 
    </sco:Collection> 
  </TextExpression.NamespacesForImplementation> 
  <TextExpression.ReferencesForImplementation> 
    <sco:Collection x:TypeArguments="AssemblyReference"> 
      <AssemblyReference>Microsoft.CSharp</AssemblyReference> 
      <AssemblyReference>System</AssemblyReference> 
      <AssemblyReference>System.Activities</AssemblyReference> 
      <AssemblyReference>System.Activities.DurableInstancing</AssemblyReference> 
      <AssemblyReference>System.Core</AssemblyReference> 
      <AssemblyReference>System.Data</AssemblyReference> 
      <AssemblyReference>System.Runtime.DurableInstancing</AssemblyReference> 
      <AssemblyReference>System.Runtime.Serialization</AssemblyReference> 
      <AssemblyReference>System.ServiceModel</AssemblyReference> 
      <AssemblyReference>System.ServiceModel.Activities</AssemblyReference> 
      <AssemblyReference>System.Xaml</AssemblyReference> 
      <AssemblyReference>System.Xml</AssemblyReference> 
      <AssemblyReference>System.Xml.Linq</AssemblyReference> 
      <AssemblyReference>WFTest.ActivityLibrary</AssemblyReference> 
      <AssemblyReference>mscorlib</AssemblyReference> 
      <AssemblyReference>WFTest.WorkflowConsole</AssemblyReference> 
    </sco:Collection> 
  </TextExpression.ReferencesForImplementation> 
  <Sequence sap2010:WorkflowViewState.IdRef="Sequence_1"> 
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_1" Text="1111111111" /> 
    <local:noResultBookmark BookmarkName="xxxx" sap2010:WorkflowViewState.IdRef="noResultBookmark_1" /> 
    <WriteLine sap2010:WorkflowViewState.IdRef="WriteLine_2" Text="2222222" /> 
    <sads:DebugSymbol.Symbol>dzREOlxUZXN0XFdGVGVzdFxXRlRlc3QuV29ya2Zsb3dDb25zb2xlXEFjdGl2aXR5MS54YW1sBy0DMg4CAQEuBS5SAgEGLwUvaAIBBDAFME8CAQIuQy5PAgEHLyovMAIBBTBDMEwCAQM=</sads:DebugSymbol.Symbol> 
  </Sequence> 
  <sap2010:WorkflowViewState.ViewStateManager> 
    <sap2010:ViewStateManager> 
      <sap2010:ViewStateData Id="WriteLine_1" sap:VirtualizedContainerService.HintSize="211,62" /> 
      <sap2010:ViewStateData Id="noResultBookmark_1" sap:VirtualizedContainerService.HintSize="211,22" /> 
      <sap2010:ViewStateData Id="WriteLine_2" sap:VirtualizedContainerService.HintSize="211,62" /> 
      <sap2010:ViewStateData Id="Sequence_1" sap:VirtualizedContainerService.HintSize="233,350"> 
        <sap:WorkflowViewStateService.ViewState> 
          <scg:Dictionary x:TypeArguments="x:String, x:Object"> 
            <x:Boolean x:Key="IsExpanded">True</x:Boolean> 
          </scg:Dictionary> 
        </sap:WorkflowViewStateService.ViewState> 
      </sap2010:ViewStateData> 
      <sap2010:ViewStateData Id="WFTest.WorkflowConsole.Activity1_1" sap:VirtualizedContainerService.HintSize="273,430" /> 
    </sap2010:ViewStateManager> 
  </sap2010:WorkflowViewState.ViewStateManager> 
</Activity>

活动 noResultBookmark

using System; 
using System.Activities; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
  
namespace WFTest.WorkflowConsole 
{ 
    public sealed class noResultBookmark : NativeActivity 
    { 
  
        public InArgument<string> BookmarkName { get; set; } 
  
        protected override bool CanInduceIdle 
        {  
            get 
            { return true; }  
        } 
  
  
        protected override void Execute(NativeActivityContext context) 
        { 
            string bookmark = context.GetValue(BookmarkName); 
  
            context.CreateBookmark(bookmark); 
  
            System.Console.WriteLine("创建bookmark:{0}", bookmark); 
        } 
    } 
}

 

控制类

 

using System; 
using System.Activities; 
using System.Activities.DurableInstancing; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.DurableInstancing; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
  
namespace WFTest.WorkflowConsole 
{ 
    public class MainClass 
    { 
  
  
        protected static AutoResetEvent reset = new AutoResetEvent(false); 
        public void Start() 
        { 
            WorkflowApplication application = new WorkflowApplication(new Activity1()); 
  
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString); 
            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30)); 
            instanceStore.DefaultInstanceOwner = view.InstanceOwner; 
  
            application.InstanceStore = instanceStore; 
  
  
  
  
            application.Completed = new Action<WorkflowApplicationCompletedEventArgs>(a => 
            { 
                application = null; 
                System.Console.WriteLine("workflowCompleted:{0}", a.CompletionState.ToString()); 
            }); 
  
  
  
            application.OnUnhandledException = a => 
            { 
                System.Console.WriteLine("unhandledException:{0}", a.UnhandledException.Message); 
                return UnhandledExceptionAction.Cancel; 
            }; 
  
  
  
            application.Aborted = a => 
            { 
                application = null; 
                System.Console.WriteLine("aborted ,Reason:{0}", a.Reason.Message); 
            }; 
  
  
  
            application.Idle = a => 
            { 
                reset.Set(); 
                Console.WriteLine("OnIdle"); 
  
            }; 
               
  
            application.Run(); 
            reset.WaitOne(); 
            application.Unload(); 
  
            application.PersistableIdle = (e) => 
            { 
                return PersistableIdleAction.Unload; 
            }; 
  
  
  
            //获取工作流实列Id 
            Guid installGuid = application.Id; 
  
        } 
  
  
        public void Load() 
        { 
  
            string bookName = "xxxx"; 
  
  
  
  
            WorkflowApplication application = new WorkflowApplication(new Activity1()); 
  
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore(Const.ConnectionString); 
            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30)); 
            instanceStore.DefaultInstanceOwner = view.InstanceOwner; 
  
            application.InstanceStore = instanceStore; 
  
  
            Guid guid = new Guid("0DAEC18F-D828-46B5-A397-D46A8A4DB4D1"); 
            application.Load(guid); 
  
  
  
            if (application.GetBookmarks().Count(p => p.BookmarkName == bookName) == 1) 
            { 
  
                application.ResumeBookmark(bookName, bookName); 
  
            } 
        } 
    } 
}

入口类

using System; 
using System.Linq; 
using System.Activities; 
using System.Activities.Statements; 
using System.Diagnostics; 
using System.Collections.Generic; 
using System.Threading; 
using System.Activities.DurableInstancing; 
using System.Runtime.DurableInstancing; 
  
namespace WFTest.WorkflowConsole 
{ 
  
    class Program 
    { 
  
  
        static void Main(string[] args) 
        { 
  
  
            MainClass main = new MainClass(); 
            //      main.Start();  //创建流程 
            main.Load();     //加载流程 
  
  
            Console.WriteLine("End"); 
  
            Console.Read(); 
        } 
  
  
    } 
}

 

 


Leave a Reply