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

How to host the Windows Workflow as a Web service(.svc)?

我正在尝试将 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 工作流服务应用程序。

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

如果您 运行 Visual Studio 中的项目,您将获得一个自动生成的 WSDL 以及您的服务操作:

它还会调出 Visual Studio 的 WCF 测试客户端工具:

您可以使用 Pick Branch activity 创建处理多种方法的基于工作流的服务。然后每个分支都有一个 Receive and Send Reply activity,接收 activity 移动到 trigger 部分,发送回复 activity 在 action部分。

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

下面是 WCF 测试客户端工具将显示的内容,其中包含从工作流中公开的多个操作:

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