Exposing WF to SOA via WCF Endpointsx

Download Report

Transcript Exposing WF to SOA via WCF Endpointsx

Code Red Solutions - building IT together
www.codered.com.au - [email protected]
EXPOSING WINDOWS WORKFLOW TO SOA:
REAL LIFE LESSONS
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Who’s this bloke?
 Paul Sorauer
 Director
 Software Architect /
Developer
 Involved professionally in
development since 2000
 Recent Clients:
 Seven Network
 Aussiepay
 E-Bisprint Pty Ltd
 Code Red Solutions




Custom Development
IT Consultancy
ISV Product Offerings
Managed Hosting Services
 Less than two years old
 Currently providing
architecture, management
and implementation services
for multiple large scale long
term projects (2 years +)
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Are we here for the free pizza?
 Let’s put it to the vote...
 Who’s here ‘cos you NEEDED that pizza?
 Who turned up ‘cos you normally do?
 Who didn’t get that hot date tonight?
 I reckon the rest of you must actually be
interested in...
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Are we here for the free pizza?
Exposing Windows Workflow to SOA: Real Life Lessons
In this 90 minute presentation, Paul Sorauer from Code Red
Solutions, will demonstrate the practical steps required to expose
Windows Workflow to SOA, and discuss the lessons learn from
real-world projects.
Service-Orientated-Architecture is becoming increasing more
important when designing ‘Enterprise Level' software solutions,
and the recent releases of Visual Studio 2008 and .Net 3.5 now
provide us with the ability to create Windows Workflows exposed
to SOA via WCF endpoints. This ability to offer workflow as a
service removes it from a tightly-coupled architecture, with the
added benefit of making it much easier to consume from
stateless environments such as ASP.NET.
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Are we here for the free pizza?
 Changes in Workflow between .NET 3.0 & 3.5
 .NET 3.5 provides full native support to run
Workflows as WCF services
 Previously hard to consume Workflow from stateless
environments (ASP.NET)
 Workflows now removed from tightly-coupled
architecture – all the client application knows it that
it’s calling a WCF service
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
What’s in it for me?
 What is Workflow and when to use it
 An overview of Sequential vs. State Machine workflows
 The basic steps required to expose a State Machine





Workflow as a WCF Service
Various ‘Gotchas' and lessons learnt the hard way;
Bugs with Visual Studio's design time support for
Workflow Services and their workarounds
Leveraging Custom Activities
The Workflow Persistence Service
Workflow support for database transactions
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Should my work flow and if so, when?
 Think of Windows Workflow as a visual designer
for your business logic
 It has a heavy emphasis on high level, re-usable,
generic, configurable activities – it’s just like a
big jigsaw puzzle, folks!
 When should things flow?
 Long running business processes – they’re easily
persisted
 Complex business logic that ‘moves’ back and forth
between states
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Should my work flow and if so, when?
 Under the covers (the simple explanation!)
 Needs to be a host process to run the Workflow
runtime
 Developer calls into the Workflow runtime and it
interacts with individual ‘instances’ of workflows
 Every instance of a workflow runs in a ‘sandbox’
inside the runtime – uniquely identified by a GUID
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
The Good, the Bad - and the Ugly later!
Pros
Cons
 Can easily see the flow of
 Can have a steep learning
logic – don’t have to read,
understand and remember
code
 Much easier to see when the
logic is incorrect or missing
 Very easy to change the logic
flow when business rules
change – as easy as drag and
drop
curve
 Extra development overhead
– not as quick as writing code
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
Sequential Workflows
State Machine Workflows
 Good for a well defined
 Is event driven
 Moves forwards and
simple process
 Pretty much forwards only
 Analogous to ‘procedural’
programming
backwards between states
 Can limit valid operations
according to ‘state’
 Analogous to... ...you
guessed it... ‘event driven’
programming
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
‘Two steps forward’ or ‘Two steps forward
and one step back’?
 Sequential Workflows
are limited and in
practice not that
applicable to realworld situations
 State Machine
Workflows have a high
degree of flexibility
and more accurately
portray the real-world
 Over time you’ll find
you mostly use state
workflows
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Let’s get flowing...
 Basic steps to create a WCF state workflow:
 It’s a WCF Service... Define the interface... Contract first






development...
Create the WCF project
Import the interface
Implement the workflow
Create a proxy service (optional)
Consume from the client
Release to IIS with ‘WorkflowServiceHostFactory’ .svc or
implement your own via inheritance if you need granular
control
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
It’s a WCF Service... Define the interface...
Contract first development...
 Let’s think about the operations you can perform on a car –
we should define an interface with the operations like:








Start Ignition
Depress Clutch
Release Clutch
Depress Gas
Release Gas
Change Gear
Depress Brake
Stop Car
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Create the WCF project
 Under WCF project types, add a State
Machine Workflow Service Library project
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Import the interface
 Reference the interface
 Import the interface
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Implement the workflow
 Create initial and final states
 Define and create remaining states
 Implement the business logic
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Create a proxy service (optional)
 WCF Workflows use ContextBindings which can
only be consumed from .NET 3.5 clients (.NET
2.0 / 3.0 with a hack)
 A proxy service will abstract that from the client
so it a becomes truly cross platform web service
 Extend the interface passing in an extra parameter of
type GUID
 Use the GUID to set the context and then pass the call
to the service and the result back to the client –
simple!
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Consume it from a client
 Add a service reference (to either the service
or the proxy service depending on your
architecture)
 You’re away!
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Release to IIS with ‘WorkflowServiceHostFactory’
.svc or implement your own via inheritance if
you need granular control
 You don’t have to – all you need is a host process
to run the WCF service
 Hosting the WCF service within IIS gives you
leverage
 The ‘WorkflowServiceHostFactory’ takes care of
starting and stopping the service as required –
you’d write that code manually otherwise
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Release to IIS with ‘WorkflowServiceHostFactory’
.svc or implement your own via inheritance if
you need granular control
 .SVC file
<%@ServiceHost language=“C#" Debug="false" Service="FULLNAMESPACE.CLASSNAME"
Factory="System.ServiceModel.Activation.WorkflowServiceHostFactory" %>
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Release to IIS with ‘WorkflowServiceHostFactory’
.svc or implement your own via inheritance if
you need granular control
 Don’t forget to copy the app.config as
web.config!
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
The Ugly...
 Large workflows become very painful
 When binding properties worklfow classes recompile after every
change – anecdotal evidence suggests C# works better than VB
 Can try to copy and paste binding objects from the properties
window – works some of the time
 Anecdotal evidence that there’s a very sour interaction between
Visual Studio and SQL Server Management Studio – VS
complains about modal dialog that needs closing – but no modal
dialog is shown – have to kill VS process
 When designing workflows ensure that SQL Server Management
Studio is closed
 Auto-save can cause excessive lag at really ill opportune
moments
 Turn auto-save off
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
The Ugly...
 Workflow bindings often complain that an
object of type xyz can’t be assigned to an
object of type xyz!
 Ignore
 If it bothers you...
 Clean the solution
 Close Visual Studio
 Run VS clean script
 Open the solution and rebuild
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
The Ugly...
 Runtime debugging support is flakey
 Never seem able to step into workflow designer
 Avoid it!
 When persistence service is enabled often can’t
step into code-behind of workflows
 Try cleaning the project
 Comment out the persistence service while
debugging
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Re-usable code – Yay!
 Custom Workflow Activities
 Good way to encapsulate your re-usable code
 Binding workflow dependency properties are a
powerful way of consuming objects
 Very easy to do with code snippets (type – ‘wdp’ /
‘wde’ for events)
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
On and on and on...
 Persistence Service
 Serializes and De-serializes entire workflow
instance to database, including the execution
status
 Just run the SQL script on the database of your
choice and put the persistence service in the
app.config
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
It’s good to be persistent, but...
 All objects, both public and private, referenced inside
workflow must be marked as serializable
 If you change the workflow you won’t be able to de-serialize
from the database – in production you’ll need to deploy
different versions
 Any unhandled errors in workflow will remove the serialized
workflow from the instance store
 You can hack the stored proc to stop this from happening
 You can use the default behaviour to your advantage – if you can’t
step into workflow when there’s an error, run SQL Profiler – the
error message is passed to the stored procedure before it deletes
the workflow instance
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Take that transaction to the bank
 You can wrap up the persistence of workflow and database writes
within the same transaction






Use the batching namespace with workflow
Create a wrapper business object with the data you need to send to the
database
Implement IPendingWork on a class
Create a new instance of your IPendingWork class, passing in an object
of the wrapper class, and add it to the
‘WorkflowEnvironment.WorkBatch’ class
In the IPendingWork class get the data out of your wrapper and save to
the database
Wrap the internals of the receive activity in a transaction scope activity
which will force both the save of the workflow into the persistence store
and your external database writes into the same transaction
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
What are we left with?
 It’s a lot of work, but it enables you to
leverage a lot of existing infrastructure,
create highly flexible custom activities with
powerful workflow binding to dependency
properties
 It’s a great technology to use in an MVC
architectural pattern
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
So what are we left with? Live Demo...
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Can you repeat that, please?









What the hell is it?
Pros and Cons
When to use it
Basic steps to get going
Pain points
Leveraging powerful reusable components
Persisting the workflow state
Database transactions
Live demo
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Questions and comments?
 Go to www.codered.com.au
 There’s a forum on the page for Q&A
 This presentation is available as a download
 Feel free to contact me if you have questions.
Code Red Solutions - building IT together
www.codered.com.au - [email protected]
Get the heck outta here!
 Hope you enjoyed the presentation
 It’s beer o’clock...