在 Windows 服务中使用 Windows Workflow Foundation 远程执行工作流
Using Windows Workflow Foundation in Windows Service for remote execution of workflow
我正在研究如何在 Windows 服务中托管 WF,同时能够与来自其他应用程序的 Windows 服务中的 WF 机制通信,指示它执行可用的工作流作为磁盘上的 xaml。 WF 机制需要能够 运行 并行工作流。
有什么我应该注意的吗?
关于哪些有用的好文章有什么建议吗?
有没有我想借用的例子 :-) ?
最好的问候
弗兰茨·汤姆森
正如我在对您的问题的评论中提到的,一种选择是使用 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 参数。
构建完成后,只需将 xaml 文件复制到您选择的位置即可。
希望对您有所帮助。
我正在研究如何在 Windows 服务中托管 WF,同时能够与来自其他应用程序的 Windows 服务中的 WF 机制通信,指示它执行可用的工作流作为磁盘上的 xaml。 WF 机制需要能够 运行 并行工作流。
有什么我应该注意的吗? 关于哪些有用的好文章有什么建议吗? 有没有我想借用的例子 :-) ?
最好的问候 弗兰茨·汤姆森
正如我在对您的问题的评论中提到的,一种选择是使用 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 参数。
构建完成后,只需将 xaml 文件复制到您选择的位置即可。
希望对您有所帮助。