c# - Windows 工作流基础中基于人工的任务

标签 c# workflow-foundation-4 workflow-foundation

我正在尝试在 Windows Workflow Foundation 4.5 中构建一个简单的请假申请应用程序,它抛出以下异常,因为工作流试图在不等待 approveRequest 事件的情况下完成。

Two SendParameters objects with same ServiceContractName and OperationName 'ApplyLeave' have different parameter names.

你能告诉我缺少什么吗?

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)},
                        },
            },
        };

        Receive approveRequest = new Receive
        {
            ServiceContractName = "ILeaveRequestService",
            OperationName = "ApproveLeave",
            CanCreateInstance = true,
            Content = new ReceiveParametersContent
            {
                Parameters ={
                    {"requestID",new OutArgument<int>(requestID)}
                }
            }
        };

        SendReply sendApproval = new SendReply
        {
            Request = receiveLeaveRequest,
            Content = new SendParametersContent
            {
                Parameters ={
                            {"approved",new InArgument<int>(0)},
                        },
            },
        };

        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,
                approveRequest,
                new WriteLine{Text="Approved"},
                sendApproval
            },
        };

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

最佳答案

您可能有两个同名的方法,它们接收两个参数。但是,由于您将对象发送到 WF 服务,因此可以将这些对象强制转换为这些方法中的任何一种,因此 WF 服务无法知道要执行其中的哪一个。

作为第一步,您应该更改服务中的“Leave”方法重载签名,而不是:

public object ApplyLeave(SomeType1 t1, SomeType2 t2) {...}
public object ApplyLeave(SomeType3 t3, SomeType4 t4) {...}

制作它:

public object ApplyLeaveA(SomeType1 t1, SomeType2 t2) {...}
public object ApplyLeaveB(SomeType3 t3, SomeType4 t4) {...}

然后在您的代码中调用您要使用的确切方法。

关于c# - Windows 工作流基础中基于人工的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43721533/

相关文章:

c# ListView :How to create Columns without header

c# - LINQ - FirstOrDefault() 然后 Select()

c# - 如何在 TFS 中反序列化和序列化构建过程参数

workflow-foundation - 在单元测试期间断言 WF 服务变量的值

visual-studio-2010 - 延迟事件在 Sharepoint 2010 工作流程中并不总是有效

c# - MySQL(或 C#)不匹配 double 或小数

c# - 使用多个提交按钮

c# - 如何从 WF 4 中的工作流调用方法?

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

WPF 还是工作流?