<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Guy Burstein : WF4</title><link>http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx</link><description>Tags: WF4</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit</title><link>http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-wcf-using-visual-studio-2010-and-net-framework-4-training-kit.aspx</link><pubDate>Tue, 19 May 2009 16:23:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9627879</guid><dc:creator>Guy Burstein</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/bursteg/comments/9627879.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bursteg/commentrss.aspx?PostID=9627879</wfw:commentRss><wfw:comment>http://blogs.msdn.com/bursteg/rsscomments.aspx?PostID=9627879</wfw:comment><description>&lt;h1&gt;WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit&lt;/h1&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit" border="0" alt="WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WFWCFusingVisua.NETFramework4TrainingKit_E64B/WF_826378c6-a02d-44ec-8662-a875643db14e.png" width="263" height="51" /&gt;&amp;#160;&amp;#160; &lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit" border="0" alt="WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WFWCFusingVisua.NETFramework4TrainingKit_E64B/WCF_449d2a51-c2ce-4abe-80d6-0a156237c6b9.png" width="310" height="47" /&gt; &lt;/p&gt;  &lt;p&gt;The &lt;strong&gt;Microsoft WF/WCF using Visual Studio 2010 and .NET Framework 4 Training Kit&lt;/strong&gt; includes presentations, hands-on labs, and demos. This content is designed to help you learn how to utilize the Visual Studio 2010 features and a variety of WF and WCF new features.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Learn &lt;strong&gt;basic workflow creation&lt;/strong&gt;, hosting and running , and test-drive workflow development. &lt;/li&gt;    &lt;li&gt;Get an introduction to workflow services, communication between workflows and content based correlation of workflow instances. &lt;/li&gt;    &lt;li&gt;Use the flexibility of &lt;strong&gt;Flowchart&lt;/strong&gt; workflow activities to implement non-sequential workflows. &lt;/li&gt;    &lt;li&gt;Learn how to &lt;strong&gt;rehost the workflow designer&lt;/strong&gt; in a WPF desktop application and customize it in several ways. &lt;/li&gt;    &lt;li&gt;Learn how you can leverage some of the main &lt;strong&gt;WF and WCF monitoring&lt;/strong&gt; features to track application execution and troubleshoot problems with services when necessary. &lt;/li&gt;    &lt;li&gt;Learn how &lt;strong&gt;Service discovery&lt;/strong&gt; allows you to locate services on the same subnet using ad hoc discovery, or using a proxy to establish connections with servers regardless of where they are. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;This version of the training kit is compatible with &lt;a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/05/18/visual-studio-2010-beta-1-is-available-for-msdn-subscribers.aspx"&gt;Visual Studio 2010 Beta 1&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Download &lt;a href="http://code.msdn.microsoft.com/wcfwf4/Release/ProjectReleases.aspx?ReleaseId=2698"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9627879" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bursteg/archive/tags/WCF/default.aspx">WCF</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/.Net+Framework+4.0/default.aspx">.Net Framework 4.0</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx">WF4</category></item><item><title>WF 4.0: Long Running Custom Activities with Bookmarks and NativeActivity</title><link>http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-4-0-long-running-custom-activities-with-bookmarks-and-nativeactivity.aspx</link><pubDate>Tue, 19 May 2009 15:40:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9627785</guid><dc:creator>Guy Burstein</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/bursteg/comments/9627785.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bursteg/commentrss.aspx?PostID=9627785</wfw:commentRss><wfw:comment>http://blogs.msdn.com/bursteg/rsscomments.aspx?PostID=9627785</wfw:comment><description>&lt;h1&gt;WF 4.0: Long Running Custom Activities with Bookmarks and NativeActivity&lt;/h1&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0: Long Running Custom Activities Bookmarks NativeActivity" border="0" alt="WF 4.0: Long Running Custom Activities Bookmarks NativeActivity" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0LongRunningCustomActivitieswithBook_C900/WF23_27f3f317-ca61-4fb3-a994-5986cb0115c8.png" width="314" height="61" /&gt;&lt;/p&gt;  &lt;p&gt;This is another post in my &lt;a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/05/19/wf-4-0-custom-activities.aspx"&gt;WF 4.0: Custom Activities series&lt;/a&gt;. In a previous post I have talked about creating a code only custom activity that performs a simple task. In this post I’ll talk about creating a more complex activities that takes inputs from the calling program during their execution.&lt;/p&gt;  &lt;p&gt;In the previous post &lt;a href="http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-4-0-code-only-custom-activities-for-atomic-actions-codeactivity-codeactivity-t.aspx"&gt;WF 4.0: Code Only Custom Activities for Atomic Actions | CodeActivity, CodeActivity&amp;lt;T&amp;gt;&lt;/a&gt; I implemented the following activity for reading a string from the console.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString2&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; value = &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; context.SetValue(Result, value);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;While this activity will perform the task it is meant to do, it is bloc&lt;strong&gt;king the calling thread and bad for scalability&lt;/strong&gt;. In addition to that It is bound to getting the value from the console only, and it is &lt;strong&gt;not generic enough&lt;/strong&gt; to support another input methods.&lt;/p&gt;  &lt;p&gt;Enter &lt;strong&gt;Bookmarks&lt;/strong&gt;.&lt;/p&gt;  &lt;h3&gt;Using NativeActivity for Complex Activities&lt;/h3&gt;  &lt;p&gt;To create a long running activity, add a new item of type &lt;strong&gt;Workflow Element&lt;/strong&gt; item to the project. Notice that the default activity inherits from &lt;strong&gt;CodeActivity&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString3&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;To create complex activities such as long running activities, me need to change the base class and inherit from &lt;strong&gt;NativeActivity&lt;/strong&gt; or &lt;strong&gt;NativeActivity&amp;lt;T&amp;gt;.&lt;/strong&gt; In addition to that, there is a difference between the type of the parameter that the &lt;strong&gt;CodeActivity&lt;/strong&gt; takes for the Execute method and the type that &lt;strong&gt;NativeActivity&lt;/strong&gt; takes. Change the input parameter type from &lt;strong&gt;CodeActivityContext&lt;/strong&gt; to &lt;strong&gt;ActivityExecutionContext&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString3&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;NativeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;ActivityExecutionContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;throw&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;NotImplementedException&lt;/span&gt;();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;h3&gt;Enter Bookmarks&lt;/h3&gt;  &lt;p&gt;In previous versions of &lt;strong&gt;Windows Workflow Foundation&lt;/strong&gt; we had the &lt;strong&gt;ExternalDataExchangeService&lt;/strong&gt; and &lt;strong&gt;WorkflowQueue&lt;/strong&gt; when we wanted to build a simple long running activity. In &lt;strong&gt;WF 4.0&lt;/strong&gt; this is much more simple to do using &lt;strong&gt;Bookmarks&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;To use bookmarks we need to:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Create a named bookmark and Implement a callback method &lt;/li&gt;    &lt;li&gt;Signal from the caller program &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Register a Named Bookmak&lt;/h3&gt;  &lt;p&gt;The following activity registers a bookmark called “input” with a callback method called &lt;strong&gt;OnBookmarkCallback&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString3&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;NativeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: #2b91af"&gt;OutArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; name;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OutArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Name&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.name; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ThrowIfOpen();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;this&lt;/span&gt;.name = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;ActivityExecutionContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;strong&gt;context.CreateNamedBookmark(&lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;, &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;BookmarkCallback&lt;/span&gt;(&lt;span style="color: blue"&gt;this&lt;/span&gt;.OnBookmarkCallback));&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;strong&gt;&lt;span style="color: blue"&gt;void&lt;/span&gt; OnBookmarkCallback(&lt;span style="color: #2b91af"&gt;ActivityExecutionContext&lt;/span&gt; context, &lt;span style="color: #2b91af"&gt;Bookmark&lt;/span&gt; bookmark, &lt;span style="color: blue"&gt;object&lt;/span&gt; obj)&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; {&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;this&lt;/span&gt;.Name.Set(context, (&lt;span style="color: blue"&gt;string&lt;/span&gt;)obj);&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; }&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;h3&gt;Signal the workflow from the caller program&lt;/h3&gt;  &lt;p&gt;In a previous post I have talked about &lt;a href="WF 4.0: How to Execute a Worklflow (WorkflowInvoker vs WorkflowInstance)"&gt;the code that is needed to execute a workflow&lt;/a&gt;. Since we are now talking about long running workflows we will start with the default code that is generated for us when we create a new sequential workflow console application that uses the &lt;strong&gt;WorkflowInstance&lt;/strong&gt; that lets know when the workflow instance is idle, and to resume a bookmark:&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;...&lt;/p&gt;    &lt;p style="margin: 0px"&gt;myInstance.Run();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: green"&gt;//Get a string from the console and resume the bookmark called “input”&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;string&lt;/span&gt; input = &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;myInstance.ResumeBookmark(&lt;span style="color: #a31515"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;, input);&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;syncEvent.WaitOne();      &lt;br /&gt;...       &lt;br /&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The main program performs the input and delivers the value to the workflow. Using this approach:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;The main program is flexible in how it wants to get values from the user and any other input source &lt;/li&gt;    &lt;li&gt;The workflow becomes idle when it is waiting for the input and during that time not consuming any resources from the machine. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;To query the available bookmarks in a workflow instance, we can use the following code:&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;IList&lt;/span&gt; bookmarks = myInstance.GetAllBookmarks();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;foreach&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;BookmarkInfo&lt;/span&gt; info &lt;span style="color: blue"&gt;in&lt;/span&gt; bookmarks)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.Write(info.BookmarkName);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9627785" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bursteg/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/.Net+Framework+4.0/default.aspx">.Net Framework 4.0</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx">WF4</category></item><item><title>WF 4.0: Code Only Custom Activities for Atomic Actions | CodeActivity, CodeActivity&lt;T&gt;</title><link>http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-4-0-code-only-custom-activities-for-atomic-actions-codeactivity-codeactivity-t.aspx</link><pubDate>Tue, 19 May 2009 14:03:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9627584</guid><dc:creator>Guy Burstein</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/bursteg/comments/9627584.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bursteg/commentrss.aspx?PostID=9627584</wfw:commentRss><wfw:comment>http://blogs.msdn.com/bursteg/rsscomments.aspx?PostID=9627584</wfw:comment><description>&lt;h1&gt;WF 4.0: Code Only Custom Activities for Atomic Actions | CodeActivity, CodeActivity&amp;lt;T&amp;gt;&lt;/h1&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" border="0" alt="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0CodeOnlyCustomActivitiesforAtomicAc_B180/WF%5B2%5D_84b491c4-5c90-4ea3-a124-672167c54855.png" width="314" height="61" /&gt;&lt;/p&gt;  &lt;p&gt;This is another post in my &lt;a href="http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/05/19/wf-4-0-custom-activities.aspx"&gt;WF 4.0: Custom Activities series&lt;/a&gt;. In this post I’ll talk about creating a code only custom activity that performs a simple task. I’ll also use input and output parameters and talk about activities that returns a single value.&lt;/p&gt;  &lt;h3&gt;A Simple Code Activity&lt;/h3&gt;  &lt;p&gt;Lets start by building a simple activity that outputs a certain string to the console. To to that, lets add a new item of type &lt;strong&gt;Workflow Element&lt;/strong&gt; (under the Workflow node in the item templates).&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" border="0" alt="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0CodeOnlyCustomActivitiesforAtomicAc_B180/image_6a34e51b-f707-4373-a8ef-bb2bca587fff.png" width="480" height="275" /&gt; &lt;/p&gt;  &lt;p&gt;Visual Studio adds a new code file (remember – this is a code-only activity) with a class that inherits from the abstract class &lt;strong&gt;System.Activities.CodeActivity&lt;/strong&gt;.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OutputString&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;To implement the logic of the atomic action, we just have to code it here. Notice that we don’t have to tell the runtime that the status of this activity is closed or something like this like we had to in WF 3.5.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OutputString&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Hello world&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;After building the project, the new custom activity will appear in the Toolbox, and can be used in the workflow.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" border="0" alt="WF 4.0  Code Custom Activities CodeActivity CodeActivity&amp;lt;T&amp;gt;" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0CodeOnlyCustomActivitiesforAtomicAc_B180/image_abaeffc1-dc75-486f-add1-2cb7fe8311ab.png" width="333" height="153" /&gt;&lt;/p&gt;  &lt;h3&gt;Adding Input Parameters to the Custom Activity&lt;/h3&gt;  &lt;p&gt;To add a new input parameter to the activity, we declare a &lt;strong&gt;InArgument&amp;lt;T&amp;gt;&lt;/strong&gt; field and property where T is the type of the parameter.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;     &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OutputString&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;{&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160; &lt;strong&gt;&lt;span style="color: #2b91af"&gt;InArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; name;&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;InArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Name&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; {&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get &lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;{ &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: blue"&gt;return&lt;/span&gt; name; &lt;/strong&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set &lt;/span&gt;&lt;/strong&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ThrowIfOpen();&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; name = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; }&lt;/strong&gt;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Hello,&amp;#160; &amp;quot;&lt;/span&gt; + &lt;strong&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.Name.Get(context)&lt;/strong&gt;);&lt;/p&gt;      &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;      &lt;p style="margin: 0px"&gt;}&lt;/p&gt;   &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Note that in the property setter implementation I am calling the base class’ &lt;strong&gt;ThrowIfOpen()&lt;/strong&gt; method. This method throws an InvalidOperationException if the activity has already been prepared for execution and the value cannot be set.&lt;/p&gt;  &lt;p&gt;Also note that in order to get the property value, we do not store the value in a private member in the class, but instead – we use the &lt;strong&gt;InArgument&lt;/strong&gt; class and using the &lt;strong&gt;Get&lt;/strong&gt; method passing it the activity context.&lt;/p&gt;  &lt;h3&gt;Output Parameters to Custom Activities&lt;/h3&gt;  &lt;p&gt;Lets create a simple activity that reads the name from the console.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;strong&gt;&lt;span style="color: #2b91af"&gt;OutArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; outputString;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;OutArgument&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt; Output&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; {&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return&lt;/span&gt; outputString; }&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;set &lt;/span&gt;{&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ThrowIfOpen();&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; outputString = &lt;span style="color: blue"&gt;value&lt;/span&gt;;&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160; }&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; value = &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;strong&gt;&amp;#160;&amp;#160;&amp;#160; context.SetValue(Output, value);&lt;/strong&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Notice how the code required for an output argument is similar to the one required for an input argument. Also notice how we set the value back to the output value using the &lt;strong&gt;ActivityContext&lt;/strong&gt; instance.&lt;/p&gt;  &lt;h3&gt;Activities with a Single Output Parameter - CodeActivity&amp;lt;T&amp;gt;&lt;/h3&gt;  &lt;p&gt;The above sample read a string from the console and placed the value in the output parameter. If this is the case we can also inherit from &lt;strong&gt;CodeActivity&amp;lt;T&amp;gt;&lt;/strong&gt;:&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ReadString2&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;CodeActivity&lt;/span&gt;&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Execute(&lt;span style="color: #2b91af"&gt;CodeActivityContext&lt;/span&gt; context)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;string&lt;/span&gt; value = &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.ReadLine();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; context.SetValue(&lt;strong&gt;Result&lt;/strong&gt;, value);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; }&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;Inheriting from &lt;strong&gt;CodeActivity&amp;lt;T&amp;gt;&lt;/strong&gt; makes it more simple to write a custom activity that has a single output parameter. It has additional benefits which I’ll talk about in a subsequent post.&lt;/p&gt;  &lt;h3&gt;Summary&lt;/h3&gt;  &lt;p&gt;In this post I talked about creating a &lt;strong&gt;code only custom activities&lt;/strong&gt; that performs a simple task. I have also talked about input and output parameters.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9627584" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bursteg/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/.Net+Framework+4.0/default.aspx">.Net Framework 4.0</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx">WF4</category></item><item><title>WF 4.0: How to Execute a Worklflow (WorkflowInvoker vs WorkflowInstance)</title><link>http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-4-0-how-to-execute-a-worklflow-workflowinvoker-vs-workflowinstance.aspx</link><pubDate>Tue, 19 May 2009 12:14:40 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9627351</guid><dc:creator>Guy Burstein</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/bursteg/comments/9627351.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bursteg/commentrss.aspx?PostID=9627351</wfw:commentRss><wfw:comment>http://blogs.msdn.com/bursteg/rsscomments.aspx?PostID=9627351</wfw:comment><description>&lt;h1&gt;WF 4.0: How to Execute a Worklflow (WorkflowInvoker vs WorkflowInstance)&lt;/h1&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This post is based on &lt;a href="http://blogs.msdn.com/bursteg/Visual%20Studio%202010%20Beta%201"&gt;Visual Studio 2010 Beta 1&lt;/a&gt; which is the latest version available in the time of writing this post, so by the time this technology ships, there are probably things that will be slight different.&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Execute Worklflow (WorkflowInvoker WorkflowInstance" border="0" alt="Execute Worklflow (WorkflowInvoker WorkflowInstance" align="right" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0HowtoExecuteaWorklflow_D783/WF_ad3238a0-eed5-4642-8b0e-bfba44d3eccb.png" width="314" height="61" /&gt;There are two ways to execute a workflow:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Using the &lt;strong&gt;WorkflowInstance&lt;/strong&gt; class &lt;/li&gt;    &lt;li&gt;Using the &lt;strong&gt;WorkflowInvoker&lt;/strong&gt; class &lt;/li&gt; &lt;/ul&gt;  &lt;h3&gt;Execute Workflows using WorkflowInstace&lt;/h3&gt;  &lt;p&gt;When you create a new &lt;strong&gt;Sequential Workflow Console Application&lt;/strong&gt;, Visual Studio creates the basic code needed to execute the workflow inside the &lt;strong&gt;Main&lt;/strong&gt; method:&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;{&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: #2b91af"&gt;AutoResetEvent&lt;/span&gt; syncEvent = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;AutoResetEvent&lt;/span&gt;(&lt;span style="color: blue"&gt;false&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; &lt;span style="color: #2b91af"&gt;WorkflowInstance&lt;/span&gt; myInstance = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WorkflowInstance&lt;/span&gt;(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Sequence1&lt;/span&gt;());&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; myInstance.OnCompleted = &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;WorkflowCompletedEventArgs&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; syncEvent.Set();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; myInstance.OnUnhandledException = &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;WorkflowUnhandledExceptionEventArgs&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(e.UnhandledException.ToString());&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: #2b91af"&gt;UnhandledExceptionAction&lt;/span&gt;.Terminate;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; myInstance.OnAborted = &lt;span style="color: blue"&gt;delegate&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;WorkflowAbortedEventArgs&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; {&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(e.Reason);&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; syncEvent.Set();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; };&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; myInstance.Run();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160; syncEvent.WaitOne();&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;1. A &lt;strong&gt;workflow instance&lt;/strong&gt; is created and gets a reference to the root activity of the workflow. The &lt;strong&gt;WorkflowInstance&lt;/strong&gt; class lets us control the running instance of the workflow and exposes several methods for this such as Run(), Abort(), Cancel(), Load(), Unload etc.&lt;/p&gt;  &lt;p&gt;2. Since the workflow execution is scheduled to another thread, we need to block the main thread until the workflow is completed or terminated. To do this, we:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Create an &lt;strong&gt;AutoResetEvent&lt;/strong&gt; initialized to false, &lt;/li&gt;    &lt;li&gt;Set the event to true when the workflow is completed, and &lt;/li&gt;    &lt;li&gt;In the main thread - wait for the workflow to complete before exiting the method. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;3. In order to handle errors in the workflow such as exceptions or termination, in addition to handling the &lt;strong&gt;OnCompleted&lt;/strong&gt; event, we are also handling the &lt;strong&gt;OnHandledException&lt;/strong&gt; and &lt;strong&gt;OnAborted&lt;/strong&gt; events. Note that when the workflow has an unhandled exception, the program can still decide how it wants to end the workflow execution, whether it is by aborting, cancelling or terminating the workflow. If the workflow is terminated, the OnCompleted event will also be raised.&lt;/p&gt;  &lt;h3&gt;Execute Workflows using WorkflowInvoker&lt;/h3&gt;  &lt;p&gt;&lt;strong&gt;WorkflowInvoker&lt;/strong&gt; lets you invoke a workflow much more easily than the previous approach. It takes the instance of the workflow or activity to run and executes it synchronously. &lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;WorkflowInvoker&lt;/span&gt;.Invoke(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Sequence1&lt;/span&gt;());&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;There are additional overloads of this method that takes parameters and a TimeSpan in which the workflow should complete within. I’ll talk in more details about passing parameters to workflow in a subsequent post, but in general this is all we have to know about &lt;strong&gt;WorkflowInvoker&lt;/strong&gt;.&lt;/p&gt;  &lt;h3&gt;Choosing between WorkflowInstance and WorkflowInvoker&lt;/h3&gt;  &lt;p&gt;If you have a simple workflow that should run synchronously – use &lt;strong&gt;WorkflowInvoker&lt;/strong&gt;. This is useful also in when unit testing workflows (more on this in a later post).&lt;/p&gt;  &lt;p&gt;If you have a &lt;strong&gt;long running workflow&lt;/strong&gt; that you want to control – persist, abort, cancel etc – use &lt;strong&gt;WorkflowInstance&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9627351" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bursteg/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/.Net+Framework+4.0/default.aspx">.Net Framework 4.0</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx">WF4</category></item><item><title>WF 4.0: Building a Hello World Sequential Workflow</title><link>http://blogs.msdn.com/bursteg/archive/2009/05/19/wf-4-0-building-a-hello-world-sequential-workflow.aspx</link><pubDate>Tue, 19 May 2009 12:10:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9627346</guid><dc:creator>Guy Burstein</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/bursteg/comments/9627346.aspx</comments><wfw:commentRss>http://blogs.msdn.com/bursteg/commentrss.aspx?PostID=9627346</wfw:commentRss><wfw:comment>http://blogs.msdn.com/bursteg/rsscomments.aspx?PostID=9627346</wfw:comment><description>&lt;h1&gt;WF 4.0: Building a Hello World Sequential Workflow&lt;/h1&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/WF_ef7c5682-700d-4d29-8c62-27a8b04ed7d7.png" width="314" height="61" /&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: This post is based on &lt;font color="#ff0000"&gt;&lt;a href="Visual Studio 2010 Beta 1"&gt;Visual Studio 2010 Beta 1&lt;/a&gt;&lt;/font&gt; which is the latest version available in the time of writing this post, so by the time this technology ships, there are probably things that will be slight different.&lt;/p&gt;  &lt;p&gt;Start &lt;font color="#ff0000"&gt;&lt;a href="Visual Studio 2010 Beta 1"&gt;Visual Studio 2010 Beta 1&lt;/a&gt;&lt;/font&gt; and create a new &lt;strong&gt;Sequential Workflow Console Application.&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_df297f03-d0a5-483b-9d2d-b194278808e2.png" width="480" height="332" /&gt; &lt;/p&gt;  &lt;p&gt;After you click OK, visual studio creates the new projects and creates a new WF project, in which there are some things you should know about:&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;img style="border-right-width: 0px; margin: 0px 0px 45px 20px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" align="right" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_dc978f71-2b47-4720-9c0f-e973e331ddca.png" width="202" height="320" /&gt;References&lt;/strong&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;System.Xaml&lt;/strong&gt; – Now that there are several technologies based on Xaml, this is a new Assembly in .Net Framework 4.0 that contains Xaml services such as serialization. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;System.Activities&lt;/strong&gt; is the assembly that contains the implementation of WF 4.0, and as you can guess, the corresponding namespace is System.Activities and anything beneath it. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;System.Activities.Design&lt;/strong&gt; contains the designers for the activities and the designer re-hosting implementation. Since the new designer is based on WPF, you can also note references to &lt;strong&gt;WindowsBase&lt;/strong&gt;, &lt;strong&gt;PresentationCode&lt;/strong&gt; and &lt;strong&gt;PresentationFramework&lt;/strong&gt;. &lt;/li&gt;    &lt;li&gt;&lt;strong&gt;System.ServiceModel&lt;/strong&gt; contains WCF implementation (as of .Net Framework 3.0) and now &lt;strong&gt;System.ServiceModel.Activities&lt;/strong&gt; contains the activities used for Workflow Services (the integration between WF and WCF). &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;Sequence.xaml&lt;/strong&gt;:&lt;/p&gt;  &lt;p&gt;By default, workflows are created declaratively in WF 4.0 and represented in &lt;strong&gt;.xaml files with no code behind&lt;/strong&gt;. If you open this file with an Xml editor, you will see it clearly.&lt;/p&gt;  &lt;div style="font-family: consolas; background: white; color: black; font-size: 11pt"&gt;   &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;?&lt;/span&gt;&lt;span style="color: #a31515"&gt;xml&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;version&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;1.0&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;encoding&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;utf-8&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt; ?&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;p:Activity&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;x:Class&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;HelloWorld.Sequence1&lt;/span&gt;&amp;quot;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns:p&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;http://schemas.microsoft.com/netfx/2009/xaml/activities&lt;/span&gt;&amp;quot;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns:s&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;clr-namespace:System;assembly=mscorlib&lt;/span&gt;&amp;quot;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns:sad&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;clr-namespace:System.Activities.Debugger;assembly=System.Activities&lt;/span&gt;&amp;quot;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns:sadx&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;clr-namespace:System.Activities.Design.Xaml;assembly=System.Activities.Design&lt;/span&gt;&amp;quot;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: red"&gt;xmlns:x&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;http://schemas.microsoft.com/winfx/2006/xaml&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;p:Sequence&lt;/span&gt;&lt;span style="color: blue"&gt; &lt;/span&gt;&lt;span style="color: red"&gt;sad:XamlDebuggerXmlReader.FileName&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;C:\...\HelloWorld\Sequence1.xaml&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;p:Sequence&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin: 0px"&gt;&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;p:Activity&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;The new WF Designer shows an empty sequence, representing an empty block of execution. Note the warning sign that says that the sequence has no child activities.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_e5f1f87b-fb12-4ca7-8c57-831e23a64e66.png" width="251" height="231" /&gt; &lt;/p&gt;  &lt;p&gt;From the &lt;strong&gt;Procedural&lt;/strong&gt; section in the &lt;strong&gt;Toolbox&lt;/strong&gt;, drag the &lt;strong&gt;WriteLine&lt;/strong&gt; Activity to the design surface. &lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_b37a9769-f994-489e-92f1-5f197935c050.png" width="259" height="213" /&gt; &lt;/p&gt;  &lt;p&gt;Now, you get a warning that the &lt;strong&gt;Text&lt;/strong&gt; property is not set. To set it, go the properties window, and open the &lt;strong&gt;Expression Editor&lt;/strong&gt;. In WF 4.0 you use the Expression to set values to variables and parameters, and you can use either static values (like in this case) or VB. &lt;strong&gt;Yes! Expression are written in VB&lt;/strong&gt;. &lt;/p&gt;  &lt;p&gt;Type the text you want to display to the console and click OK.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_a55edc5b-969b-4248-90c7-6ffba4770792.png" width="450" height="273" /&gt; &lt;/p&gt;  &lt;p&gt;Now, that the simple workflow is completed, you can use Ctrl + F5 to run it like you normally do.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_cefd08b7-090b-4f36-92ad-4f63b204792f.png" width="480" height="250" /&gt; &lt;/p&gt;  &lt;h3&gt;Debugging a Workflow&lt;/h3&gt;  &lt;p&gt;The debugging experience when debugging a workflow in WF 4.0 is very similar to the debugging experience when using code. You can right click an activity the in designer and select &lt;strong&gt;Insert Breakpoint&lt;/strong&gt;, or use &lt;strong&gt;F9&lt;/strong&gt; to do this.&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_fafdf8e7-f0ae-466f-878e-f952b2397b42.png" width="480" height="392" /&gt; &lt;/p&gt;  &lt;p&gt;Once you do this and run the workflow, the debugger highlights the current activity using a yellow border (similar to the yellow background for the current statement when debugging code).&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; margin: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="WF 4.0 Hello World Sequential Workflow" border="0" alt="WF 4.0 Hello World Sequential Workflow" src="http://blogs.msdn.com/blogfiles/bursteg/WindowsLiveWriter/WF4.0BuildingaHelloWorldSequentialWorkfl_CBBD/image_1164933d-d9bf-47e4-95b5-aa30be08d7d6.png" width="243" height="184" /&gt; &lt;/p&gt;  &lt;p&gt;Enjoy!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9627346" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/bursteg/archive/tags/Visual+Studio+2010/default.aspx">Visual Studio 2010</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/.Net+Framework+4.0/default.aspx">.Net Framework 4.0</category><category domain="http://blogs.msdn.com/bursteg/archive/tags/WF4/default.aspx">WF4</category></item></channel></rss>