【问题标题】:How to host the Windows Workflow as a Web service(.svc)?如何将 Windows 工作流托管为 Web 服务 (.svc)?
【发布时间】:2017-10-14 22:40:23
【问题描述】:

我正在尝试将 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();
            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

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

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

    以下是 WCF 测试客户端工具将显示的内容以及从工作流中公开的多个操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-08
      • 2015-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多