DOC

COMHOL10 - Roles Transactions Compensation And Exceptions

By Lewis Kelly,2014-09-09 15:53
9 views 0
COMHOL10 - Roles Transactions Compensation And Exceptions

Hands-On Lab

    Lab Manual

    Lab 10 Roles, Transactions, Compensation,

    Exceptions

Please do not remove this manual from the lab

     ;

    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, SQL 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.

     ;

Contents

    LAB 10: ROLES, TRANSACTIONS, COMPENSATION, EXCEPTIONS ................................................................. 1 Lab Objective ......................................................................................................................................................... 1 Exercise 1 Using Roles and managing Exceptions in a Workflow ...................................................................... 1 Task 1 Create a new Workflow Project ........................................................................................................... 2 Task 2 Build the workflow ............................................................................................................................... 2 Task 3 Add an Exception Handler to workflow .............................................................................................. 4 Task 4 Building, Running and Validating the Roles Workflow ........................................................................ 5 Exercise 2 Atomic Transactions in a Workflow ................................................................................................... 6 Task 1 Set up the solution .............................................................................................................................. 6 Task 2 Create the Workflow............................................................................................................................ 7 Task 3 Run the Application ............................................................................................................................. 9 Exercise 3 Compensation ................................................................................................................................. 11 Task 1 Modify the Workflow Project ............................................................................................................. 11 Task 2 - Test the application ............................................................................................................................ 13 Lab Summary ....................................................................................................................................................... 14

    Page i

Lab 10: Roles, Transactions, Compensation, Exceptions

    Estimated time to complete this lab: 60 minutes

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

Lab Objective

    The objective of this lab is to demonstrate the role authorization, exceptions, transactions and compensation features of Windows Workflow Foundation.

    After completing this lab, you will be able to:

     Programmatically work with roles in the context of a workflow application in authorizing the

    execution of workflow activities.

     Use atomic and long-running transactions with compensating behavior in the context of a

    workflow application

     Manage exceptions within a workflow

Exercise 1 Using Roles and managing Exceptions in a Workflow

    Windows Workflow Foundation supports the execution of activities based on the membership of a user in a given role. In this exercise, you will add role based authorization to a workflow scenario. The workflow scenario that is illustrated is the initiation of a purchase order. You will create a workflow which requests a purchase order. The initiator will only be able to raise a purchase order if they are part of the WindowsRole that is configured for the EventSink activity.

    You will create a workflow which has two activities. The first is a code activity which creates a Role of valid PO Initiators as a list of Windows Accounts. The second activity is an Event Sink activity which is provided as in the resources folder. The EventSink activity is created with one interface to receive an event raised by the main program. The main program raises this event to request the initiation of a Purchase Order. You will have to ensure that the Role of valid PO Initiators includes the user name and machine name that you are logged on as.

    If the user logged in is part of the Role of POInitiator then the workflow completes successfully. If the user is not part of Role then an exception is thrown by the EventSink activity that is caught by the workflow exception handler.

    Page 1

    Task 1 Create a new Workflow Project

1. Copy the solution RolesWorkflow folder

    From source directory: C:\Windows Workflow Foundation\Labs\Lab10\Resources\Exercise1

    To destination directory: C:\Windows Workflow Foundation\Labs\Lab10

    2. Open the solution file PurchaseOrderWorkflow.sln under C:\Windows Workflow

    Foundation\Labs\Lab10\RolesWorkflow

    3. The POInterface project defines the interface used for communicating information from the host.

    The EventSink activity that is in the workflow uses this the POInterface defined in this project

    Task 2 Build the workflow

    1. Build the solution so the POInterface and the PurchaseOrderWorkflow projects get built.

    2. Select the designer view by double-clicking Workflow1.cs in the Solution Explorer

    3. Add a Code activity to the workflow by dragging and dropping one from the Toolbox. 4. Rename the Code1 activity to SetupRoles in the Properties windows for this activity. 5. Create an ExecuteCode handler by name AfterActivation for the SetupRoles Code activity by

    typing the name AfterActivation for ExecuteCode property

    6. Select the designer view by double-clicking Workflow1.cs in the Solution Explorer. Add an

    EventSink Activity to the workflow and drag it under the SetupRoles Activity of the workflow

    7. Select the properties windows of eventSinkActivity1 activity

    8. Due to a current known issue you might need to close the Visual Studio solution after saving it

    and then reopen it again so that the Type Browser for InterfaceType shows the newly built

    interface IStartPurchaseOrder its associated event InitiatePurchaseOrder.

    9. Set the properties for the eventSinkActivity1 as follows:

    ID should be set be InitiatePO name

    InterfaceType should be set to IStartPurchaseOrder under the referenced POInterface

    assembly

    EventName should be set to InitiatePurchaseOrder by selecting from the drop-down list

    Roles should be set to poInitiators by typing the value in.

    Invoked Handler should be set to AfterInitiation by typing the value in. The handler

    AfterInitiation will be created in Workflow1.cs file. Add the following line to this

    method:

     Console.WriteLine("Purchase Order initiated successfully");

    10. Now your workflow should look like the following:

    Page 2

    11. Add the following code in the AfterActivation method (Inserting the code snippets is another

    option. You can insert one using the context menu (right click) by selecting “Insert Snippet…”

    double clicking on “My Code Snippets” and then double click on “Windows Workflow Foundation Labs” then open the “Lab10” snippets

List<NTAccount> poInitiatorsList = new List<NTAccount>();

string localMachineName = System.Environment.MachineName;

string userDomainName = System.Environment.UserDomainName;

    string userName = System.Environment.UserName;

    // Change the variable userDomainName to the localMachineName // if the current user is logged onto to the local machine poInitiatorsList.Add(new NTAccount(userDomainName, userName));

poInitiatorsList.Add(new NTAccount(localMachineName, "LOCALUSER1"));

poInitiatorsList.Add(new NTAccount(localMachineName, "LOCALUSER2"));

poInitiatorsList.Add(new NTAccount(localMachineName, "LOCALUSER3"));

WindowsRole poInitiatorsRole = new WindowsRole(poInitiatorsList);

    // Add the role to the RoleCollection representing the POInitiators

poInitiators.Add(poInitiatorsRole);

    12. In the following line change the variable userDomainName to the variable localMachineName if

    the current user is logged onto to the local machine poInitiatorsList.Add(new NTAccount(userDomainName, userName));

    Page 3

Task 3 Add an Exception Handler to workflow

    1. Select the workflow and right click to view the Exception Handlers

    2. Select and drag an Exception Handler to the workflow exception. It will be named as

    ExceptionHandler1.

    Page 4

    3. Select the properties windows for the ExceptionHandler1 and make the following changes

     For the Type property for ExceptionHandler1, specify

    System.Workflow.Runtime.WorkflowAuthorizationException

     For the variable property ExceptionHandler1, specify authorizationException

    4. Select and drag a Code Activity into the Exception Handler1. Rename the Code1 activity that

    appears in Workflow Exceptions view to excepHandlerCode

     Type in authExceptionHandler for the ExecuteCode property. This will create the Code

    handler for this activity named as authExceptionHandler

    5. Add the following line to the method authExceptionHandler

    Console.WriteLine("Exception message: {0}",

    authorizationException.Message.ToString());

    Task 4 Building, Running and Validating the Roles Workflow

1. Save the solution and build

    2. To add the local users referenced in the poInitiators Role, run the batch file addlocalusers.bat at

    C:\Windows Workflow Foundation\Labs\Lab10\Resources\Exercise1\RolesWorkflow from the

    command prompt

    3. Run the workflow by pressing Ctrl-F5

    4. You should see the following:

    Page 5

    Purchase Order initiated by: {loggedOnUser}

    Purchase Order initiated successfully

    Press any key to continue . . .

    Where {loggedOnUser} is the user is who is currently logged on

    5. Comment out the following line in the AfterActivation();method in Workflow1.cs

    poInitiatorsList.Add(new NTAccount(userDomainName, userName));

    6. Build and run the workflow by pressing Ctrl-F5

    7. You should see the following:

    Purchase Order initiated by: {loggedOnUser}

    Exception message: Authorization failed for message received for event activity

    InitiatePO, from principal {loggedOnUser}.

    Press any key to continue . . .

    Exercise 2 Atomic Transactions in a Workflow

    In this exercise, you will learn how to use atomic transactions in a workflow. Atomic transactions exhibit ACID (Atomicity, Consistency, Isolation and Durability) properties that can enlist in transactions (say in the context of SQL databases). You will create a workflow to model moving money from one bank account to a second bank account. The workflow will consist of a sequence of activities. In the first activity money is withdrawn from bank account A. In the second activity money is deposited in bank account B. In the third activity an exception is thrown. All three activities are wrapped in a TransactionalContext activity which has the Transaction property set to Atomic. The two code activities perform the money transfer operation by using ADO.NET to access a SQL Server database that you create as part of the exercise.

    You will observe that the database activities are rolled back at the end of the workflow and you will observe that when the transactional behavior is disabled, then the database transaction does not get rolled back.

    Task 1 Set up the solution

    1. Open a command window and change your directory to C:\Windows Workflow

    Foundation\Labs\Lab10\Resources\Exercise2 to run setupdatabase.bat script to create the

    Bank database we will use for this exercise. The script first creates a database with a name

    “Bank” (if the database is already created, it drops it). Bank database will have two tables, with

    two columns each. We will be recording banking transactions on those tables.

    ChequeAccount SavingsAccount

    ChequeAccountId SavingsAccountId

    Balance Balance

    Page 6

    2. Since we will be working with transactions, we will need a persistence mechanism to persist the

    workflow instance as part of the transaction. Please run the script SetupPersistenceService.bat

    in the same folder to setup the SqlPersistenceService service‟s store. The service is added by

    default in your host application.

    3. Copy the solution AtomicTransactionsWorkflow folder from

    Source directory C:\Windows Workflow Foundation\Labs\Lab10\Resources\Exercise2

    To destination directory: C:\Windows Workflow Foundation\Labs\Lab10

    4. Open the solution file AtomicTransactionsWorkflow.sln under C:\Windows Workflow

    Foundation\Labs\Lab10\AtomicTransactionsWorkflow

    5. If SQL Express is configured on the machine, the connection string variable sqlConnection in

    Workflow1.cs and the connection string parameter to SqlStatePersistenceService in

    Program.cs should be qualified with the instance name.

     For e.g. the modified connection string should look like this:

     "Integrated Security=SSPI;Persist Security Info=False;Initial

    Catalog=WorkflowStore;Data Source=.\\SQLExpress"

    6. Specifying just the default 'localhost', '.' or 'computername' in the connection string as given

    above will not work as they apply only to Microsoft SQL Server

    Task 2 Create the Workflow

    1. Double Click on Workflow1.cs from the Solution Explorer if it is not already displayed. 2. From the Toolbox | Windows Workflow menu, drag the TransactionalContext activity and drop

    it into the box marked Drop Activities here to create a Sequential Workflow. 3. Set the Transaction property of the TransactionalContext activity to Atomic

    4. From the Toolbox menu, drag the Code activity and drop it into the box marked Drop Activities

    Here for TransactionalContext activity

    5. In the Properties window for Code1 activity set the properties as follows:

     (ID) Debit

     (ExecuteCode) select DebitCodeHandler from the dropdown listbox

    6. Your workflow should look like this:

    Page 7

Report this document

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