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

时间:2017-05-16 02:10:38

标签: 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();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

最快的方法是创建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服务的主题可以非常复杂。 :)