在Windows服务中使用Windows Workflow Foundation来远程执行工作流

时间:2015-08-31 10:30:46

标签: workflow-foundation-4

我正在调查如何在Windows服务中托管WF,同时能够与其他应用程序中的Windows服务中的WF机制进行通信,指示它在磁盘上执行xaml可用的工作流程。 WF机制需要能够并行运行工作流。

我应该注意什么? 关于哪些好文章会有所帮助的任何建议? 我会借用的任何例子:-)?

最诚挚的问候 Franz Thomsen

1 个答案:

答案 0 :(得分:0)

正如我在您的问题的评论中提到的,一种选择是使用WCF来托管端点,该端点在被调用时将执行您的工作流程。

作为一个基本示例,创建一个简单的WCF层。此处的端点接受2个参数,向您显示将参数传递给WCF并传递到WF的示例。然后它创建一个WF应用程序,重新保存.xaml文件并执行它。

using System;
using System.Activities;
using System.Activities.XamlIntegration;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace WCFForWorkflowsExample
{
    /// <summary>
    /// basic WCF layer - added references to System.Activities and System.Xaml
    /// </summary>
    public class Service1 : IService1
    {
        /// <summary>
        /// create a thread blocking event to allow the wf enough time to complete.
        /// </summary>
        private AutoResetEvent ev;

        /// <summary>
        /// As n example this operation accepts a workflow name and a param
        /// </summary>
        /// <param name="workflowName"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public bool RunWorkflow(int workflowId, string param)
        {
            //create the blocking event.
            ev = new AutoResetEvent(false);

            //adde
            try
            {
                //using the workflow id lets determine soemhow which workflow you plan on running.
                //lets assume your storing the xaml file location (on disk) for each wf associated with an id
                //but in this scenario lets hardcode the location to the local C.
                var xamlPath = "C:/wf.xaml";

                //you can then envoke the xaml file.

                Activity workflow = ActivityXamlServices.Load(xamlPath);

                //create a dict to pass the param to our wf 
                // have to construct a dictionary
                //this has to be the same name as an InArgument<object> Argument on our xaml file.
                var input = new Dictionary<string, object> { { "paramName", param} };

                //create a medium to run the wf - force the syncronisation to be on the current thread which will mean one wf gets run at a time rather than multi threaded.
                var wfApp = new WorkflowApplication(workflow, input)
                {
                    Completed = e =>
                       {
                           if (e.CompletionState == ActivityInstanceState.Faulted)
                           {
                               //you could get the output from the wf
                           }
                           else if (e.CompletionState == ActivityInstanceState.Canceled)
                           {

                           }
                           ev.Set();
                       },
                    OnUnhandledException = e =>
                    {
                        //log error if you wanted to 
                        return UnhandledExceptionAction.Terminate;

                    },
                    SynchronizationContext = SynchronizationContext.Current
                };

                wfApp.Run();

                //wait for the wf to complete.
                ev.WaitOne();

                return true;
            }
            catch (Exception ex)
            {
                //log an error
                return false;
            }
        }
    }
}

基于以下界面。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFForWorkflowsExample
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        bool RunWorkflow(int workflowId, string param);
    }
}

最后确保您有一个可以执行的xaml文件。确保您已正确命名In参数。

enter image description here

构建完成后,只需将xaml文件复制到您选择的位置即可。

希望这有帮助。