Partilhar via


Task 2: Create the Classes in the Workflow Library

In this task, you create the workflow and communication classes in the workflow library project. The library project includes the following classes:

  • The workflow class, created in two files (LoanApprovalWorkflow.cs and LoanApprovalWorkflow.designer.cs).

  • The communication service's parent interface (IEventService).

  • The communication service class (EventService).

  • The communication service's event argument class (LoanEventArgs).

  • An enumeration used to define the type of events (LoanEventTrackingData).

To create the class source code files

  1. In Solution Explorer, right-click your solution and select Add, and then New Project…. Select Workflow as the project type and select SequentialWorkflowLibrary as the template. Name the project WorkflowLibrary. In the new project, rename Workflow1 to LoanApprovalWorkflow, and confirm that references should be updated. Add four new class files to the project, named ILoanEventService, LoanEventService, LoanEventArgs, and LoanEventTrackingData.

To add code to the files

  1. Replace the code in the ILoanEventService file with the following code. ILoanEventService is the interface used by the workflow to receive calls from the communication service.

    using System;
    using System.Workflow.Activities;
    
    namespace WorkflowLibrary
    {
        [ExternalDataExchange]
        public interface ILoanEventService
        {
            event EventHandler<LoanEventArgs> Approved;
        }
    }
    
  2. Replace the code in the LoanEventArgs file with the following code. The LoanEventArgs class derives from ExternalDataEventArgs and is used to send information from the host to the workflow.

    using System;
    using System.Workflow.Activities;
    
    namespace WorkflowLibrary
    {
        [Serializable]
        public class LoanEventArgs : ExternalDataEventArgs
        {
            private String approvedValue;
    
            public String Approved
            {
                get { return approvedValue; }
                set { approvedValue = value; }
            }
    
            public LoanEventArgs(String approved, Guid instanceId)
                : base(instanceId)
            {
                Approved = approved;
            }
        }
    }
    
  3. Replace the code in the LoanEventService file with the following code. The LoanEventService class is the implementation of ILoanEventService that the application uses to send messages to the workflow.

    using System;
    
    namespace WorkflowLibrary
    {
        public class LoanEventService : ILoanEventService
        {
            public void RaiseApproved(String approved, Guid instanceId)
            {
                LoanEventArgs args = new LoanEventArgs(approved, instanceId);
                if (this.Approved != null)
                {
                    Approved(null, args);
                }
            }
    
            public event EventHandler<LoanEventArgs> Approved;
        }
    }
    
  4. Open the LoanApprovalWorkflow file in the designer. Open the toolbox by selecting View, Toolbox. Add the activities in the following table to the workflow.

    Activity Type Activity Name

    Code Activity

    CodeTrackRequest

    HandleExternalEvent activity

    HandleExternalEventActivityApproveLoan

    Code Activity

    CodeTrackApproval

  5. Double-click the CodeTrackRequest activity to create an event handler for the ExecuteCode event of the activity in the workflow. Add the following code to the new event handler to store tracking data concerning the new loan request.

    this.TrackData(LoanEventTrackingData.ApplicantName.ToString(), this.ApplicantName);
                this.TrackData(LoanEventTrackingData.LoanAmount.ToString(), this.LoanAmount);
    
  6. Double-click the CodeTrackApproval activity to create an event handler for the ExecuteCode event of the activity in the workflow. Add the following code to the new event handler to store tracking data concerning the new loan approval.

    this.TrackData(LoanEventTrackingData.Approved.ToString(), HandleExternalEventActivityApproveLoan_e1.Approved);
    
    
  7. Select the HandleExternalEventActivityApproveLoan activity, and select the button next to the InterfaceType property. In the dialog that appears, select ILoanEventService as the interface type and select OK.

  8. Set the EventName for the HandleExternalEventActivityApproveLoan activity to Approved.

  9. In the properties pane for the HandleExternalEventActivityApproveLoan activity, select the button in the e field. Select the Bind to a New Member tab, and select the Create Property button. Select OK to create the new property in the workflow and bind it to the activity.

  10. Add the following properties in the LoanApprovalWorkflow file.

    • ApplicantName of type String, with Get and Set methods that access a private member called applicantNameValue.

    • LoanAmount of type String, with Get and Set methods that access a private member called loanAmountValue.

    private String applicantNameValue;
    
            public String ApplicantName
            {
                get { return applicantNameValue; }
                set { applicantNameValue = value; }
            }
    
            private String loanAmountValue;
    
            public String LoanAmount
            {
                get { return loanAmountValue; }
                set { loanAmountValue = value; }
            }
    
  11. Replace the code in the LoanEventTrackingData file with the following code. The LoanEventTrackingData enumeration is used by the workflow to specify what type of tracking data is recorded, and is used by the application to differentiate between different pieces of tracking data retrieved from the tracking database.

    using System;
    
    namespace WorkflowLibrary
    {
        public enum LoanEventTrackingData
        {
            ApplicantName,
            LoanAmount,
            Approved
        }
    }
    

Compiling the Code

For more information about compiling your code, see Compiling the Code.

See Also

Concepts

Local Communication and Correlation Overview
Windows Workflow Foundation and Application Communication
Using Local Services in Workflows

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04