Sei sulla pagina 1di 31

Visual Studio® 2008:

Windows® Workflow
Foundation
Module 5: Creating and Managing Runtime Services
• Creating a Custom Runtime Service

• Hydrating and Dehydrating Workflows

• Monitoring Workflows

• Tracking Workflows
Lesson: Creating a Custom Runtime Service
• Overview of Runtime Services

• Options for Creating Custom Runtime Services

• Creating a Custom Runtime Service

• Adding a Service to the Runtime

• Removing a Service from the Runtime

• Demonstration: Creating a Custom Runtime Service


Overview of Runtime Services
A runtime service is a class that provides additional
functionality to the workflow runtime engine

Predefined service classes:


WorkflowRuntimeService

ExternalDataExchangeService

WorkflowCommitWorkBatchService

WorkflowLoaderService

WorkflowPersistenceService

WorkflowSchedulerService

TrackingService
Options for Creating Custom Runtime Services
There are two ways to create new services that work
within Windows Workflow Foundation:
• Extend a base service
• Create an entirely new custom service

Why extend a base service?


• To fine-tune default service behavior that is used implicitly
by the workflow runtime engine

Why create an entirely new custom service?


• To implement additional service functionality
• To control when the service is started and stopped
Creating a Custom Runtime Service
To create a custom runtime service
• Define a class that inherits from WorkflowRuntimeService
or a derived class
• Override methods from the base class
• Use the Runtime property to view and modify the workflows and
services that are running in the workflow runtime engine

public class MyService : WorkflowRuntimeService


{
...
}
Adding a Service to the Runtime
To add a service to the workflow runtime engine:
• Create a WorkflowRuntime object
• Create an instance of your service object
• Call the AddService method on the WorkflowRuntime object
• Start the workflow runtime engine

using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())


{
MyService myService = new MyService();
workflowRuntime.AddService(myService);

workflowRuntime.StartRuntime();

}
Removing a Service from the Runtime
To remove a service from the workflow runtime engine:

• Stop the workflow runtime engine


• Call the RemoveService method on the workflow runtime
engine
• Restart the workflow runtime engine

workflowRuntime.StopRuntime();
workflowRuntime.RemoveService(myService);
workflowRuntime.StartRuntime();
Demonstration: Creating a Custom Runtime Service
In this demonstration, you will see how to:
• Create a custom runtime service class

• Add the custom runtime service to a workflow runtime


engine
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Hydrating and Dehydrating Workflows
• Overview of Long-Running Persistent Workflows

• Persistence Services

• Creating and Configuring a Persistence Database

• Adding the Persistence Service to the Workflow Runtime


Engine
• Demonstration: Hydrating and Dehydrating Workflows
Overview of Long-Running Persistent Workflows
Business processes might take a long time to complete

• A business process might be idle for significant periods of


time, waiting for input from users or other systems

The workflow runtime engine can unload a workflow


instance from memory during periods of inactivity
• The workflow runtime engine uses the persistence service
to persist the state of long-running workflows
Persistence Services
A persistence service is a class that inherits from
WorkflowPersistenceService

• SqlWorkflowPersistenceService is a predefined persistence


service that persists workflow state to a database

Dehydration
Persisted state
Hydration

Persistence database
Workflow runtime engine with
SqlWorkflowPersistenceService
Creating and Configuring a Persistence Database
To create a database for SqlWorkflowPersistenceService,
execute a SQL statement such as the following:
CREATE DATABASE MyWorkflowPersistenceStore

To configure the database, run the following SQL scripts:


• SqlPersistence_Schema.sql
• SqlPersistence_Logic.sql

Location of SQL scripts:

• %WINDIR%\Microsoft.NET\Framework\v3.0\
Windows Workflow Foundation\SQL\<language>
Adding the Persistence Service to the Workflow
Runtime Engine
<!-- Configuration file -->
<Services>
<add type="…SqlWorkflowPersistenceService…"
connectionString="…"
UnloadOnIdle="true"/>
</Services>

// Application code.
using (WorkflowRuntime runtime = new WorkflowRuntime())
{
string connectionString = … ;

SqlWorkflowPersistenceService persistenceService =
new SqlWorkflowPersistenceService(connectionString,
true,
TimeSpan.MaxValue,
new TimeSpan(0, 2, 0));

workflowRuntime.AddService(persistenceService);


}
Demonstration: Hydrating and Dehydrating Workflows
In this demonstration, you will see how to:
• Create a workflow that demonstrates the need for
hydration and dehydration
• Create a persistence database

• Add the persistence service to the workflow runtime


engine
• Examine the effects of hydration and dehydration
Notes Page Over-flow Slide. Do Not Print Slide.
See Notes pane.
Lesson: Monitoring Workflows
• Overview of Workflow Monitoring

• Disabling Performance Counters

• Demonstration: Viewing Workflow Performance Counters


Overview of Workflow Monitoring
You can monitor the performance of workflows
• Profile workflow creation, loading and unloading, and completion
• Enables you to identify performance bottlenecks

Windows Workflow Foundation defines two sets of


performance counters
• Overall totals
• Rates / second
Disabling Performance Counters
Windows Workflow Foundation performance counters are
enabled by default
• To disable performance counters, set the
EnablePerformanceCounters workflow property to false

<configuration>

<CustomWorkflowRuntimeSettings Name="Workflow1"
EnablePerformanceCounters="false">

</CustomWorkflowRuntimeSettings>

</configuration>
Demonstration: Viewing Workflow Performance
Counters
To view workflow performance counters:
• Use the Reliability and Performance Monitor (PerfMon.exe) utility
• Specify the Windows Workflow Foundation counters that you want
to view

You can view performance counters in three ways:


• Line graph
• Histogram
• Report
Lesson: Tracking Workflows
• Overview of Workflow Tracking

• Creating and Configuring a Tracking Database

• Adding the Tracking Service to the Workflow Runtime


Engine
• Defining Tracking Profiles

• Querying Tracking Data

• Demonstration: Tracking Workflows


Overview of Workflow Tracking
Windows Workflow Foundation enables you to track
events that are raised during workflow execution
• Useful diagnostic information during operations management

Types of tracking data available:


• Activity events
• Workflow events

A tracking service is a class that inherits from


TrackingService

• SqlTrackingService is a predefined tracking service that stores


tracking data in a database
Creating and Configuring a Tracking Database
To create a database for SqlTrackingService, execute a
SQL statement such as the following:
CREATE DATABASE MyTrackingStore

To configure the database, run the following SQL scripts:


• Tracking_Schema.sql
• Tracking_Logic.sql

Location of SQL scripts:

• %WINDIR%\Microsoft.NET\Framework\v3.0\
Windows Workflow Foundation\SQL\<language>
Adding the Tracking Service to the Workflow
Runtime Engine
<!-- Configuration file -->
<Services>

<add type="…SqlTrackingService…" connectionString="…" />

</Services>

// Application code.
using (WorkflowRuntime runtime = new WorkflowRuntime())
{
string connectionString = … ;

SqlTrackingService trackingService =
new SqlTrackingService(connectionString);

workflowRuntime.AddService(trackingService);
}
Defining Tracking Profiles
You can define a tracking profile to filter tracking data
• You can create a tracking profile programmatically by using the
tracking profile object model
• Alternatively you can specify a tracking profile declaratively as XML

<?xml version="1.0"?>
<TrackingProfile ...>

<!-- Specify the activities and events that you want to track -->

</TrackingProfile>

To add the tracking profile to the database:


• Call the UpdateTrackingProfile stored procedure on the
tracking database
Querying Tracking Data
Query tracking data by using the SqlTrackingQuery class
• Get activity events
• Get workflow instance events

SqlTrackingQuery query = new SqlTrackingQuery(connectionString);

SqlTrackingWorkflowInstance workflowInstance;
query.TryGetWorkflow(workflowInstanceId, out workflowInstance);

// Get activity events.


foreach (ActivityTrackingRecord r in workflowInstance.ActivityEvents)

{

}

// Get workflow instance events.


foreach (WorkflowTrackingRecord r in workflowInstance.WorkflowEvents)
{

}
Demonstration: Tracking Workflows
In this demonstration, you will see how to:
• Create a tracking database

• Add the tracking service to the workflow runtime engine

• Query tracking data


Lab: Creating and Managing Runtime Services
• Exercise 1: Creating a Custom Runtime Service

• Exercise 2: Hydrating and Dehydrating Workflows

• Exercise 3: Monitoring Workflows

• Exercise 4: Tracking Workflows


(If Time Permits)

Logon information

Virtual machine 6462A-LON-DEV-05

User name Student

Password Pa$$w0rd

Estimated time: 45 minutes


Lab Review
• What are the advantages and disadvantages of using the
same database for the persistence and tracking services?
• Why is CancellationWorklfow a good candidate for
hydration and dehydration?
• Why is CreateAppointmentWorkflow a good candidate for
tracking?
Module Review and Takeaways
• Review Questions

• Real-World Issues and Scenarios

• Best Practices

Potrebbero piacerti anche