DOC

COMHOL05 - Communicating with the Host Application

By Brandon Henderson,2014-09-09 15:49
5 views 0
COMHOL05 - Communicating with the Host Application

Hands-On Lab

    Lab Manual

    Lab 05 Communicating with the Workflow Host

    Application

Please do not remove this manual from the lab

    Page i

    Information in this document, including URL and other Internet Web site references, is subject to change without notice. This document supports a preliminary release of software that may be changed substantially prior to final commercial release, and is the proprietary information of Microsoft Corporation.

    This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, AS TO THE INFORMATION IN THIS DOCUMENT.

    The entire risk of the use or the results from the use of this document remains with the user. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

    Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

    Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.

? 2005 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, Windows Server, Visual C# and Visual Studio are either registered

    trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.

    The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

    Page ii

Contents

    LAB 05: COMMUNICATING WITH THE HOST APPLICATION........................................................................... 2 Lab Objective .................................................................................................................................................. 2 Exercise 1 Communicating with a Local Service ............................................................................................ 2

    Task 1 Create the Host Application ........................................................................................................... 2

    Task 2 Define the Local Service................................................................................................................ 3

    Task 3 Define the Workflow ....................................................................................................................... 6 Exercise 2 Correlating Communication........................................................................................................ 11

    Task 1 Enabling the Interface for Correlation .......................................................................................... 12

    Task 2 Modifying the Workflow with an Additional Voter .......................................................................... 12 Lab Summary ................................................................................................................................................ 17

    Page 1

Lab 05: Communicating with the Host Application

    Estimated time to complete this lab: 45 minutes

    The project files for this lab are in the c:\Windows Workflow Foundation\Labs\Lab05 folder.

Lab Objective

    The objective of this lab is to introduce Windows Workflow Foundation messaging and local services. Messaging involves an event system implemented by Windows Workflow Foundation to be used by workflows and the host of the runtime. There are two main scenarios for how events are used. First, there is the DoSomething method / SomethingDone event pattern where the first is a method invoked on the local service and the latter is an event raised back to state that the request has gone through and completed. Typically, in this scenario, the DoSomething method which runs in the local service will start up a worker thread which then performs some work. This worker thread in turn will raise the event to indicate that the work has been completed.

    Second, there is more generic inbound event scenario. Consider that DoSomething method takes an extended or unknown period to do the work, e.g. a document is assigned to someone to review. In this scenario the host itself is a generic raiser of events. The person assigned to review the document gets an e-mail with a document to review. The user reviews the document and clicks some button to indicate approval or rejection. The host gets information from the button click that is associated with the document review, e.g. workflow instance ID, and uses this to do a reflection based method call that raises an event.

    After completing this lab, you will be able to:

    ; Create a simple local service that exposes a method in which it does some work and also

    publishes an event which it fires when its work is complete.

    ; Use the Listen activity to wait for one of two events to be raised by the local service

    ; Understand the basics of correlating multiple EventSink and InvokeMethod activities to a single

    event and method

    Exercise 1 Communicating with a Local Service

    Task 1 Create the Host Application

    1. Open Visual Studio 2005 by going to the Start Menu | Programs | Microsoft Visual Studio

    2005 Beta 2 | Microsoft Visual Studio 2005 Beta 2

    2. In Visual Studio 2005, select the File | New | Project menu command.

    3. Select Visual C# | Workflow | Sequential Workflow Console Application. Change the Name

    to CommunicationsLab. You can accept the default location C:\Windows Workflow

    Foundation\Labs\Lab05\VotingWorkflow.

    4. Add System.Windows.Forms as a reference.

    5. In the program.cs file, set the using statements to the following:

    using System;

    Page 2

using System.Threading;

    using System.Workflow.Runtime;

    6. In the program.cs file, delete the code in the Program class, and replace it with the following

    code. It is fundamentally the same as the generated template code but has additional

    functionality for displaying the creation and completion of a workflow instance.

    static AutoResetEvent waitHandle = new AutoResetEvent(false); static VotingService votingService;

static void Main(string[] args)

    {

     WorkflowRuntime workflowRuntime = new WorkflowRuntime();

     votingService = new VotingService();

     workflowRuntime.AddService(votingService);

     workflowRuntime.StartRuntime();

     workflowRuntime.WorkflowCreated += new

    EventHandler(OnWorkflowCreated);

     workflowRuntime.WorkflowCompleted += new

    EventHandler(OnWorkflowCompleted);

     Type type = typeof(VotingWorkflow);

     workflowRuntime.StartWorkflow(type);

     waitHandle.WaitOne();

     workflowRuntime.StopRuntime();

    }

    static void OnWorkflowCreated(object sender, WorkflowEventArgs e) {

     Console.WriteLine("Workflow " + e.WorkflowInstance.InstanceId.ToString() + " created.");

    }

    static void OnWorkflowCompleted(object sender, WorkflowCompletedEventArgs e) {

     Console.WriteLine("Workflow " + e.WorkflowInstance.InstanceId.ToString() + " completed.");

     waitHandle.Set();

    }

    Task 2 Define the Local Service

    1. Add a code file to the project by right clicking the project and choosing Add | New Item

    2. Choose a Code File and name the file VotingService.cs

    Page 3

    3. VotingService.cs will be a blank file. Copy and paste the following code to define the message, interface, and local service used by the workflow. The VotingEventArgs class defines the message passed between the service and the workflow and stores the instance id of the workflow as well as the alias of the person that voting on the proposal. The IVotingService is an interface that defines how the service and workflow communicate. The workflow calls the service to create a new ballot using the CreateBallot method; the service raises the events into the workflow to indicate whether the person approved or rejected the proposal. using System;

    using System.Threading;

    using System.Workflow.ComponentModel;

    using System.Workflow.Runtime;

    using System.Workflow.Runtime.Messaging;

    using System.Windows.Forms;

namespace CommunicationsLab

    {

     [Serializable]

     internal class VotingEventArgs : WorkflowMessageEventArgs

     {

     private string alias;

     public VotingEventArgs(Guid InstanceID, string alias) :

    base(InstanceID)

     {

     this.alias = alias;

     }

    Page 4

     public string Alias

     {

     get { return this.alias; }

     set { this.alias = value; }

     }

     }

     [DataExchangeService]

     internal interface IVotingService

     {

     event EventHandler ApproveProposal;

     event EventHandler RejectProposal;

     void CreateBallot(string alias);

     }

     internal class VotingService : IVotingService

     {

     public event EventHandler ApproveProposal;

     public event EventHandler RejectProposal;

     public void CreateBallot(string alias)

     {

     Console.WriteLine("Ballot created for " + alias + ".");

     ThreadPool.QueueUserWorkItem(ShowVotingDialog, new VotingEventArgs(BatchEnvironment.CurrentInstanceId, alias));

     }

     public void ShowVotingDialog(object o)

     {

     DialogResult result;

     VotingEventArgs votingEventArgs = o as VotingEventArgs;

     Guid instanceId = votingEventArgs.InstanceId;

     string alias = votingEventArgs.Alias;

     result = MessageBox.Show("Approve Proposal, " + alias + "?", alias

    + " Ballot", MessageBoxButtons.YesNo);

     if (result == DialogResult.Yes)

     {

     if (ApproveProposal != null)

     ApproveProposal(null, votingEventArgs);

     }

     else

     {

     if (RejectProposal != null)

     RejectProposal(null, votingEventArgs);

     }

     }

     }

    }

    Page 5

    Task 3 Define the Workflow

    The workflow defines the voting logic for a proposal. For this workflow all proposals must be

    approved by Jim and the workflow manages when a ballot is created and completed. It

    creates a ballot for Jim and then waits for either an approval or rejection of the proposal.

    1. Rename the Workflow1.cs file to VotingWorkflow.cs. Since we have not built the solution yet

    you may get a dialog stating the solution does not build in its current state, press Cancel to

    continue.

    2. Double click the VotingWorkflow.cs file to bring up the Visual Studio 2005 designer for Windows

    Workflow Foundation, referred to as the Visual Studio workflow designer in the remainder of this

    document.

    3. Rename the workflow from workflow1 to VotingWorkflow.

    4. Right click the VotingWorkflow.cs file and choose View Code. Copy and paste the bolded code

    into the VotingWorkflow class.

    public sealed partial class VotingWorkflow : SequentialWorkflow

    {

     public VotingWorkflow()

     {

     InitializeComponent();

     }

     private void AfterJimApproved(object sender, EventArgs e)

     {

     Console.WriteLine("Proposal Approved by Jim.");

     }

     private void AfterJimRejected(object sender, EventArgs e)

     {

     Console.WriteLine("Proposal Rejected by Jim.");

     }

    }

    5. Double click the VotingWorkflow.cs file to bring up the Visual Studio workflow designer. 6. The ballot is created by calling the VotingService.CreateBallot method. To call a method on a

    local service from a workflow choose the InvokeMethod activity from the toolbox and drag it on

    to the workflow.

    Page 6

    7. Change the ID of the InvokeMethod activity from invokeMethodActivity1 to CreateBallotForJim

    (this exact name must be used or the workflow logic copied in a later step will not function

    properly.)

    8. Note, the activity’s validation logic is indicating we need to set several of the activity properties.

    To call a method on a local service we first need to set the interface property. Choose the

    ellipsis (…) next to InterfaceType and choose the IVotingService interface in the project.

    Page 7

    9. Once an interface is selected, the method to invoke can be set. To do this, choose the

    CreateBallot method name for the MethodName property.

10. The CreateBallot method takes a single string parameter called alias. Type in Jim for the value

    of this property.

    11. The workflow now will create a ballot for Jim but it needs to get the response back. To do this,

    create add a Listen activity after the InvokeMethod activity, name it WaitForJimResponse.

    Rename the left branch of the listen activity to WaitForJimApproval and the right branch to

    WaitForJimRejection. The Listen activity will wait for a response from one inbound event before

    proceeding. In this workflow it will wait for the ballot response from Jim i.e. to approve or

    reject the proposal.

    Page 8

Report this document

For any questions or suggestions please email
cust-service@docsford.com