workflow - 如何将 Windows 工作流作为 Web 服务 (.svc) 托管?

标签 workflow workflow-foundation-4 workflow-foundation window-functions sharepoint-workflow

我正在尝试将 Windows 工作流作为 Web 服务托管,下面是我构建的示例工作流,并希望作为 Web 服务(.svc)托管,您能否建议所需的步骤?

using System;
using System.ServiceModel.Activities;
using System.Activities;
using System.ServiceModel;
using System.Activities.Statements;

namespace DemoWF
{
    public class _25_LeaveRequest
    {
        public WorkflowService GetInstance()
        {
            WorkflowService service;
            Variable<int> empID = new Variable<int> { Name = "empID" };
            Variable<int> requestID = new Variable<int> { Name = "requestID" };

        Receive receiveLeaveRequest = new Receive
        {
            ServiceContractName = "ILeaveRequestService",
            OperationName = "ApplyLeave",
            CanCreateInstance = true,
            Content = new ReceiveParametersContent
            {
                Parameters ={
                    {"empID",new OutArgument<int>(empID)}
                }
            }
        };

        SendReply replyLeaveRequestID = new SendReply
        {
            Request = receiveLeaveRequest,
            Content = new SendParametersContent
            {
                Parameters ={
                            {"requestID",new InArgument<int>(requestID)},
                        },
            },
        };


        Sequence workflow = new Sequence()
        {
            Variables = { empID, requestID },
            Activities = {
                new WriteLine{Text="WF service is starting..."},
                receiveLeaveRequest,
                new WriteLine{
                    Text=new InArgument<string>(aec=> "Emp ID="+empID.Get(aec).ToString())
                },
                new Assign<int>{
                    Value=new InArgument<int>(5),
                    To=new OutArgument<int>(requestID)
                },
                new WriteLine{
                    Text=new InArgument<string>(aec=> "Request ID="+requestID.Get(aec).ToString())
                },
                replyLeaveRequestID
            },
        };

        service = new WorkflowService
        {
            Name = "AddService",
            Body = workflow
        };
        return service;
    }
}

现在,它是自托管的,如下所示
namespace DemoWF
{
    class Program
    {
        static void Main(string[] args)
        {
            LeaveRequest();
        }

        private static void LeaveRequest()
        {
            _25_LeaveRequest receiveAndReplyWorkflow = new _25_LeaveRequest();
            WorkflowService wfService = receiveAndReplyWorkflow.GetInstance();
            Uri address = new Uri("http://localhost:8000/WFServices");
            WorkflowServiceHost host = new WorkflowServiceHost(wfService, address);

            try
            {
                Console.WriteLine("Opening Service...");
                host.Open();

                Console.WriteLine("WF service is listening on " + address.ToString() + ", press any key to close");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("some thing bad happened" + e.StackTrace);
            }
            finally
            {
                host.Close();
            }
        }
    }
}

最佳答案

最快的方法是创建 WCF 工作流服务应用程序。

Creating a WCF Workflow Service Application...

您将获得一个工作流设计器,您可以在其中拖放所需的事件:

Boilerplate workflow from project template...

如果您在 Visual Studio 中运行该项目,您将获得带有服务操作的自动生成的 WSDL:

The running workflow-as-a-WCF-service...

它还将启动 Visual Studio 的 WCF 测试客户端工具:

enter image description here

您可以使用 Pick Branch 创建处理多种方法的基于工作流的服务。事件。每个分支都会有一个 Receive and Send Reply事件,接收事件移至 trigger部分和 action 中的发送回复事件部分。

每个触发器都针对服务的特定操作。在下面的例子中,我定义了两个操作:MyFirstOperationMySecondOperation .

enter image description here

下面是 WCF 测试客户端工具将通过工作流公开的多个操作显示的内容:

enter image description here

希望这能让你开始。建立基于工作流的 WCF 服务的主题可能会涉及很多。 :)

关于workflow - 如何将 Windows 工作流作为 Web 服务 (.svc) 托管?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43991454/

相关文章:

git - 我的项目使用了 100 多个 git 子模块,哪个子模块替代方案可以优雅地处理大量存储库

使用 JIT 编译器的 Julia 工作流程

ruby-on-rails - Rails - 同一模型上有多个 "workflows"

workflow-foundation-4 - 如何在 Workflow 4 中将参数从一个事件传递给另一个事件

configuration - 在 Web.config 中添加工作流扩展

c# - 作为管道的 RX IObservable

c# - 并行执行单个工作流实例

.net - WF4 AssignActivity - 多行语句中断设计器

webforms - 如何在 ASP .Net 中开发将遵循工作流中定义的过程的 Web 应用程序?

c# - 无状态(在谷歌代码上)和 Windows 工作流之间的比较