Sei sulla pagina 1di 30

Session Code: CV.

27
Integrating WCF and
Workflow, Advanced activity
development
Marcel de Vries
Microsoft MVP Team System
http://blogs.infosupport.com/marcelv
marcelv@infosupport.com
Agenda
Integrating WCF in workflow
Building activities basics
Design time behavior
Type Converters
Activity Designer
Runtime behavior
Activity Code Generator
Additional features
Attached Dependency properties
Activity Validator
Summary
Integrating WCF in workflow
WCF requires a class that implements a service
contract and that is hosted in a service host
[ServiceContract(Namespace="http://www.beyonddotnet.com/WCFTestInterface")]
public interface WCFTEstInterface
{
[OperationContract]
string SayHello();
[OperationContract(IsOneWay=true, Action="DoNothing2")]
void DoNothing();
}
public class WCFTEstInterfaceImpl : WCFTEstInterface
{
public string SayHello(){}
public void DoNothing(){}
}
Workflows are classes derived from activity
and are hosted in a WorkflowRuntime
How can we combine the two?
Create activities that can hold the service
operation definitions
Generate a class that implements the interfaces
and applies the correct attributes
Create an implementation that can bootstrap the
load/creation of a workflow when a WCF call
comes in
Integrating WCF in workflow
Workflow Host e.g IIS
Integrating WCF in workflow
Generated code
handling
the incoming call
Load/Create WF
Process WF
Return result
Client
Activity Component Model
Each activity has an associated set of components
Components are associated through attributes on the Activity Definition
If not specified, parents attributes are applied
Required
Optional
(defaults provided)
[Designer(typeof(MyDesigner))]
[ActivityCodeGenerator(typeof(MyCodeGen))]
[ActivityValidator(typeof(MyValidator))]
public class MyActivity: Activity {...}
Activity
Code
Generator
Designer
Validator
Serializer
Services
Workflow Runtime
Building the activities
Design time behavior
Pick interface using WF type picker dialog
Create a dropdown containing methods available on Interface
Add parameter that can be bound based on selected method in
interface
Generate WCF code that handles incoming call
Validate if activities are configured correctly
Runtime behavior
Register an event queue to wait on until notified by generated
code to start processing
At continuation bind parameters based on binding expressions
Output activity returns results
Implementing Design time
behavior
Design time behavior
Interface Property
Implementing the Interface property
Dependency property of type Type
Attached a TypeBrowserEditor using attributes
Need to implement ITypeFilter to show only interfaces we are
interested in
public bool CanFilterType(Type type, bool throwOnError)
{
// Display only interfaces
return (type.IsInterface);
}
[Browsable(false)]
public string FilterDescription
{
get { return "Choose an interface you want to expose as a ServiceContract"; }
}
Design time behavior
Method Property
Method property needs a custom value provider,
since it must show all methods available in the
selected interface
Done by adding attribute to property providing custom
TypeProvider
When a method is selected in the grid you need
to trigger that the parameters are exposed to the
property grid as bindable properties
Triggering is done by specifying attribute to the
Method attribute
[RefreshProperties(RefreshProperties.All)]
And is handled in the designer OnActivityChanged
Design time behavior
Parameters property
Parameters property needs to be a dynamic
list of bindable parameters depending on the
available arguments of the selected Method
in the interface
This is done by creating the Parameters property
of type WorkflowParameterBindingCollection
and implementing designer behavior
Designer takes care of adding bindable properties
to this collection in the PreFilterProperties
method
Design time behavior
Implementing the designer
PreFilterProperties must use reflection to get
all parameters for the selected method
Adds a new PropertyDescriptor for each
parameter
Eacht property is Bindable in the property grid
Each property has the BindUITypeEditor
attached
Designtime behavior
WCF Output activity must have the ability
to be linked to an input activity
Same as the Method property in the input
activity, but now other logic
walks the activity tree and returns all the input
activities found.
DEMO:Creating the design time
behavior
Implementing Design time behavior for
WCFActivities
Type Property
Method Property
Parameters property
Implementing Runtime behavior
Runtime behavior must do the following
For each incoming message load or create the
appropriate workflow
Yield the thread to the workflow so it can execute until
it becomes idle in the WCF Input activity
Register a synchronization object when output is
generated by the workflow
There is an associated output activity
Yield the thread again to execute the workflow
between the input and output activity
Binds the message data to parameters set in the designer
Communicate back the results of the call when
synchronization object is signaled
Workflow Host e.g IIS
Generated code
handling
the incoming call
Load/Create WF
Queue Message
Data
Return result
Yield Thread
Register and
wait on queue
Register WaitObject
Yield Thread
Dequeue &
Bind params
Wait for WaitObject
Implementing Runtime behavior
WCFInput Must register a queue to receive the data
Done using a base class that handles queue management.
Only Implement ProcessQueueItem
Generated code will pass message data for
processing in the queue
protected override bool ProcessQueueItem(ActivityExecutionContext context,
object item)
{
object[] inputParameters = (object[])item;
ProcessParameters(inputParameters);
return true;
}
Implementing Runtime behavior
ProcessParameters binds the provided
message data to the workflow properties
private void ProcessParameters(object[] inputParameters)
{
MethodInfo methodInfo = InterfaceType.GetMethod(MethodName);
int parameterIndex = 0;
foreach (ParameterInfo paramInfo in methodInfo.GetParameters())
{
if (Parameters.Contains(paramInfo.Name))
{
WorkflowParameterBinding parameterBinding = Parameters[paramInfo.Name];
parameterBinding.Value = inputParameters[parameterIndex++];
}
}
}
Activity metadata
WCF message data
Demo
Show generic WCF call handling
Show Input activity implementation
Implementing Runtime behavior
Most of the WCF handling can be
programmed in a base class that handles
each call
Only parameter binding is done in the input
activity itself, rest is WCF plumbing
Some code needs to be generated to expose
the service contract
This is done using an ActivityCodeGenerator
Generate a class that implements the selected method
signatures in the input activities
Add WCF attributes to those methods so they can be
exposed to WCF
Implementing runtime behavior
Activity Code Generator
Invoked during the compilation cycle of a
workflow project
Use CodeDom to add you custom code
Use reflector to see what you generated
WF Program compilation steps:
1. XAML deserialization & code generation
2. Activity Validation
3. Activity code Generation
4. Code Compilation
Implementing runtime behavior
What needs to be generated?
The call to HandleWCFCall for each input
activity in the workflow
[ServiceContract(Name="TestWorkflow1")]
public class TestWorkflow1_wcfContractImpl : WCFCallHander
{ // Methods
[OperationContract(Action="ActivatingCall")]
private string ActivatingCall()
{
object[] parameters = new object[0];
return (string) base.HandleWCFCall(true, false, typeof(TestWorkflow1),
"WCFTEstInterface", "ActivatingCall", "System.String", parameters);
}

}
Activity code generation
Done by building an
ActivityCodeGenerator
You receive a codedom representation of
your workflow implementation
Search for a definition of a class called
<workflowType>_wcfimpl
If not found create one using code dom and adding
it to the namenspace
Add a new method signature using code dom
to the generated class
Demo
Show generation using
ActivityCodeGenerator
Additional Designtime features
Attached Properties
WCF has some settings you would like to set
once on each workflow
Namespace, ServiceName, GeneratedClassName
You can use attached properties for this
purpose
Requires designer to add IExtenderProvider during
initialization for each attached property
Requires a ExtenderProvider for each property
Value can be retrieved using the GetValue()
method on the activity you attached the property to
Additional Designtime Features
Designer Validation
A validator can check if the properties that got assigned to your
activity are correct
Requires you to implement a class derived from ActivityValidator
You implement the Validate method
You apply the validator to the activity using the ActivityValidator
attribute
At validation you have the full instantiated activity tree at your
disposal
Can do complex validation cross the tree
Check if an input activity has an associated output activity when retrun
value != void
Your validator is activated in compilation cycle!
Therefore validation is normally used for metadata validation
26
Demo
Attached properties
Activity Validator
Codeplex version
Codeplex version contains additional feature
Correlation based on attributes
Leaves the client from knowing and returning the
message headers
At the Data/Operation contract an attribute is
added that will do the correlation
Done using a WCF behavior
Required few changes in code gen, since there
you need to add attributes to the service
operations
Q&A
More information
Activities shown today
www.codeplex.com/WCFActivities
http://blogs.infosupport.com/marcelv
MSDN forums
Workflow SDK
Evaluation form
Vul je evaluatieformulier in en maak kans
op een van de prachtige prijzen!!
Fill out your evaluation form and win one of
the great prizes!!
Session Code:CV.27

Potrebbero piacerti anche