Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Monday, 06 August 2012

“…provides an API, an in-process workflow engine, and a rehostable designer to implement long-running processes as workflows within .NET applications”
http://en.wikipedia.org/wiki/Windows_Workflow_Foundation

“Any time you have a long running operation like this WF4 is a good possibility. The fact that the graphical designer allows you to show the actual running process, not a Visio copy of it, is also a huge benefit.

There is however a learning curve to WF4 and there are times you have to do things the WF4 way instead of the C#/VB way you did before. That said there is certainly a benefit, thinks like an approval request not being handled in, lets say, 14 days is very easy to do in WF4.”
http://stackoverflow.com/questions/4344467/beginner-wf4-question-when-is-wf4-useful-really

Great place to start for leaning WF4
http://blogs.msdn.com/b/rjacobs/archive/2010/10/13/want-to-learn-windows-workflow-4-wf4.aspx

Hands on Lab – Intro to WF4 by Ron Jacobs

·         How to create an activity using the designer and XAML or C# code
·         How to run and test activities using the WorkflowApplication and WorkflowInvoker classes
·         How to pass InArguments and receive OutArguments from a workflow
·         How Expressions and Variables are used
·         How to use the Sequence, WriteLine, Assign and If activities
·         How to load and run an activity from a xaml file

Ex1 / 2 – Hello World
image

class Program
    {
        static void Main(string[] args)
        {
            WorkflowInvoker.Invoke(new SayHello());
        }
    }

Simplest WF app after changing xaml to SayHello and the property.

Ex3 – CodeActivities

Meat for code short and sweet…  AsyncCodeAcitivity for longer running

public sealed class SayHelloInCode : CodeActivity
    {
        protected override void Execute(CodeActivityContext context)
        {
            Console.WriteLine("Hello Workflow 4 in code");
        }
    }

Then in program, call Invoke(new SayHelloInCode())

Ex4 – Dynamic Workflows with XAML

WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"));
            Console.ReadLine();

take out MSBuild:Compile, content (so build copys xaml to debug) and

Ex5 – Testing and Personalised Greeting

Personalised greeting..testable.

[TestMethod]
        public void ShouldReturnGreetingWithName()
        {
            IDictionary<string, object> output;
            output = WorkflowInvoker.Invoke(
                                    new SayHello()
                                    {
                                        UserName = "Test"
                                    });

            Assert.AreEqual("Hello Test from Workflow 4", output["Greeting"]);
        }

Wrote the test first

image
Added in arguments.  Inbound username, and outbound Greeting

static void Main(string[] args)
        {
            //WorkflowInvoker.Invoke(new SayHelloInCode());

            //WorkflowInvoker.Invoke(ActivityXamlServices.Load("SayHello.xaml"));
            //Console.ReadLine();

            Console.Write("Enter your name: ");
            string name = Console.ReadLine();
            Console.WriteLine(GenerateGreeting(name));
            Console.WriteLine("Thank you for using WF4 - press any key to exit");
            Console.ReadLine();
        }

        static string GenerateGreeting(string name)
        {
            var output = WorkflowInvoker.Invoke(new SayHello() { UserName = name });
            return (string)output["Greeting"];
        }

Simple wire up.

Ex6 – WorkflowApplicaiton and MultiThread

Instead of WorkflowInvoker.Invoke – doesn’t support lots of things.. need a more advanced hosting class.  Designed (not messaging activities)..
Want to generate the personalised greeting on a background thread eg it could be being done by a slow webservice.

image
Trying to assign value to out argument ActivityThread, but can’t do this with assign activity, as I have one already. So need a Sequence activity.

[TestMethod]
public void ShouldRunActivityOnBackgroundThread()
{
    var completedEvent = new AutoResetEvent(false);
    IDictionary<string, object> output = null;

    output = WorkflowInvoker.Invoke(
       new SayHello()
       {
           UserName = "Test"
       });

    Assert.IsTrue(output.ContainsKey("ActivityThread"),
        "SayHello must return an OutArgument named ActivityThread");

    // Don't know for sure what it is yet
    object outarg = output["ActivityThread"];

    Assert.IsInstanceOfType(outarg, typeof(Int32), "ActivityThread must be of type Int32");

    int activityThread = (Int32)outarg;

    Console.WriteLine("Test thread is {0}", Thread.CurrentThread.ManagedThreadId);
    Console.WriteLine("Activity thread is {0}", activityThread);

    Assert.IsTrue(activityThread > 0, "Invalid value for ActivityThread");

    Assert.AreNotEqual(Thread.CurrentThread.ManagedThreadId, activityThread,
        "Activity was not invoked on background thread");
}

Test fails as not occurring on background thread, as Workflow.Invoker is doing it on current thread.

Ex7 If Else Logic

[TestMethod]
public void ShouldReturnGenericGreeting()
{
    IDictionary<string, object> output;
    output = WorkflowInvoker.Invoke(
        new SayHello()
        {
            UserName = ""
        });
    Assert.AreEqual("Greetings from Workflow 4", output["Greeting"]);
}

image

Dev Days 2010 – Matt Milner Pluralsight

**emailled him**Good but can’t find demos live.

WF4.

  • What is WF
  • What does it give me as a dev
  • Why do I want to use WF

What is WF:

  • A framework for declarative programming ie write logic but not in code
  • Tools
  • Runtime

Tradeoff speed for features.

WCF is for making remote calls.. maybe webservices…

Simplify

  • encapsulate complex control flow

WhyWF4 Demo

image

Custom Activity called GetOData which calls WCF data service and get some data
WF will manage all the begin end stuff…

Message correlation.. new if WF4

Problem domains

any multi step process

**state machine which needs update http://code.msdn.microsoft.com/Windows-Workflow-b4b808a8

*books – there are books on Amazon on WF4.

| | #