2017-05-01 69 views
7

我想在Windows工作流基礎4.5中建立一個簡單的請假申請,它會引發以下異常,因爲工作流試圖完成而無需等待approveRequest活動。基於人的任務在Windows工作流基礎

具有相同ServiceContractName和 的兩個SendParameters對象OperationName'ApplyLeave'具有不同的參數名稱。

您能否建議我缺少什麼?

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

回答

0

提到你可能有兩種方法具有相同的名字,其接受兩個參數主辦。但是,由於您將對象發送到WF服務,因此可以將這些對象轉換爲其中一種方法,因此WF服務無法知道要執行哪一個。

正如你應該改變你的「離開」的方法第一步重載簽名服務,這樣反而有:

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) {...} 

並在代碼中調用確切的方法,你想使用。

+0

謝謝你的寶貴意見,它有幫助。 –

+0

不客氣:)我很高興這有幫助。 –

相關問題