<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.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>Omar AL Zabir blog on ASP.NET Ajax and .NET 3.5 : workflow</title><link>http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx</link><description>Tags: workflow</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Web 2.0 AJAX Portal using jQuery, ASP.NET 3.5, Silverlight, Linq to SQL, WF and Unity</title><link>http://msmvps.com/blogs/omar/archive/2009/04/08/web-2-0-ajax-portal-using-jquery-asp-net-3-5-silverlight-linq-to-sql-wf-and-unity.aspx</link><pubDate>Wed, 08 Apr 2009 18:16:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1685991</guid><dc:creator>omar</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/omar/rsscomments.aspx?PostID=1685991</wfw:commentRss><comments>http://msmvps.com/blogs/omar/archive/2009/04/08/web-2-0-ajax-portal-using-jquery-asp-net-3-5-silverlight-linq-to-sql-wf-and-unity.aspx#comments</comments><description>&lt;p&gt;&lt;a title="Dropthings Web 2.0 style AJAX portal" href="http://dropthings.omaralzabir.com" target="_blank"&gt;Dropthings&lt;/a&gt; – my &lt;a title="Dropthings - Open Source AJAX Portal" href="http://www.codeplex.com/dropthings" target="_blank"&gt;open source&lt;/a&gt; Web 2.0 Ajax Portal has gone through a technology overhauling. Previously it was built using ASP.NET AJAX, a little bit of Workflow Foundation and Linq to SQL. Now Dropthings boasts full &lt;strong&gt;jQuery&lt;/strong&gt; front-end combined with ASP.NET AJAX &lt;code&gt;UpdatePanel&lt;/code&gt;, &lt;strong&gt;Silverlight widget&lt;/strong&gt;, full &lt;strong&gt;Workflow Foundation&lt;/strong&gt; implementation on the business layer, 100% &lt;strong&gt;Linq to SQL Compiled Queries&lt;/strong&gt; on the data access layer, Dependency Injection and Inversion of Control (IoC) using &lt;strong&gt;Microsoft Enterprise Library 4.1&lt;/strong&gt; and &lt;strong&gt;Unity&lt;/strong&gt;. It also has a ASP.NET AJAX Web Test framework that makes it real easy to write Web Tests that simulates real user actions on AJAX web pages. This article will walk you through the challenges in getting these new technologies to work in an ASP.NET website and how performance, scalability, extensibility and maintainability has significantly improved by the new technologies. Dropthings has been licensed for commercial use by prominent companies including BT Business, Intel, Microsoft IS, Denmark Government portal for Citizens; Startups like Limead and many more. So, this is serious stuff! There’s a very cool open source implementation of Dropthings framework available at &lt;a title="AJAX Portal at National University of Singapore" href="http://nexus.nus.edu.sg/apicta" target="_blank"&gt;National University of Singapore&lt;/a&gt; portal.&lt;/p&gt;  &lt;p&gt;Visit: &lt;a href="http://dropthings.omaralzabir.com"&gt;http://dropthings.omaralzabir.com&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_57907290.png"&gt;&lt;img title="Dropthings AJAX Portal" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="529" alt="Dropthings AJAX Portal" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_37551A39.png" width="600" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;I have published a new article on this on CodeProject:&lt;/p&gt;  &lt;p&gt;&lt;a title="Web 2.0 AJAX Portal using jQuery, ASP.NET 3.5, Silverlight, Linq to SQL, WF and Unity" href="http://www.codeproject.com/KB/ajax/Web20Portal.aspx"&gt;http://www.codeproject.com/KB/ajax/Web20Portal.aspx&lt;/a&gt;&lt;/p&gt;  &lt;h2&gt;Get the source code&lt;/h2&gt;  &lt;p&gt;Latest source code is hosted at Google code:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://code.google.com/p/dropthings"&gt;http://code.google.com/p/dropthings&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There’s a CodePlex site for documentation and issue tracking:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://www.codeplex.com/dropthings"&gt;http://www.codeplex.com/dropthings&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;You will need Visual Studio 2008 Team Suite with Service Pack 1 and Silverlight 2 SDK in order to run all the projects. If you have only Visual Studio 2008 Professional, then you will have to remove the Dropthings.Test project.&lt;/p&gt;  &lt;h2&gt;New features introduced&lt;/h2&gt;  &lt;p&gt;Dropthings new release has the following features:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Template users – you can define a user who’s pages and widgets are used as a template for new users. Whatever you put in that template user’s pages, it will be copied for every new user. Thus this is an easier way to define the default pages and widgets for new users. Similarly you can do the same for a registered user. The template users can be defined in the &lt;code&gt;web.config&lt;/code&gt;. &lt;/li&gt;    &lt;li&gt;Widget-to-Widget communication – Widgets can send message to each other. Widgets can subscribe to an Event Broker and exchange messages using a Pub-Sub pattern. &lt;/li&gt;    &lt;li&gt;WidgetZone – you can create any number of zones in any shape on the page. You can have widgets laid in horizontal layout, you can have zones on different places on the page and so on. With this zone model, you are no longer limited to the Page-Column model where you could only have N vertical columns. &lt;/li&gt;    &lt;li&gt;Role based widgets – now widgets are mapped to roles so that you can allow different users to see different widget list using &lt;code&gt;ManageWidgetPersmission.aspx.&lt;/code&gt; &lt;/li&gt;    &lt;li&gt;Role based page setup – you can define page setup for different roles. For ex, Managers see different pages and widgets than Employees. &lt;/li&gt;    &lt;li&gt;Widget maximize – you can maximize a widget to take full screen. Handy for widgets with lots of content. &lt;/li&gt;    &lt;li&gt;Free form resize – you can freely resize widgets vertically. &lt;/li&gt;    &lt;li&gt;Silverlight Widgets – You can now make widgets in Silverlight! &lt;/li&gt; &lt;/ul&gt;  &lt;h2&gt;Why the technology overhauling&lt;/h2&gt;  &lt;p&gt;Performance, Scalability, Maintainability and Extensibility – four key reasons for the overhauling. Each new technology solved one of more of these problems. &lt;/p&gt;  &lt;p&gt;First, jQuery was used to replace my personal hand-coded large amount of Javascript code that offered the client side drag &amp;amp; drop and other UI effects. jQuery already has a rich set of library for Drag &amp;amp; Drop, Animations, Event handling, cross browser javascript framework and so on. So, using jQuery means opening the door to thousands of jQuery plugins to be offered on Dropthings. This made Dropthings highly extensible on the client side. Moreover, jQuery is very light. Unlike AJAX Control Toolkit jumbo sized framework and heavy control extenders, jQuery is very lean. So, total javascript size decreased significantly resulting in improved page load time. In total, the jQuery framework, AJAX basic framework, all my stuffs are total 395KB, sweet! Performance is key; it makes or breaks a product.&lt;/p&gt;  &lt;p&gt;Secondly, Linq to SQL queries are replaced with Compiled Queries. Dropthings did not survive a load test when regular lambda expressions were used to query database. I could only reach up to 12 Req/Sec using 20 concurrent users without burning up web server CPU on a Quad Core DELL server. &lt;/p&gt;  &lt;p&gt;Thirdly, Workflow Foundation is used to build operations that require multiple Data Access Classes to perform together in a single transaction. Instead of writing large functions with many if…else conditions, for…loops, it’s better to write them in a Workflow because you can visually see the flow of execution and you can reuse Activities among different Workflows. Best of all, architects can design workflows and developers can fill-in code inside Activities. So, I could design a complex operations in a workflow without writing the real code inside Activities and then ask someone else to implement each Activity. It is like handing over a design document to developers to implement each unit module, only that here everything is strongly typed and verified by compiler. If you strictly follow Single Responsibility Principle for your Activities, which is a smart way of saying one Activity does only one and very simple task, you end up with a highly reusable and maintainable business layer and a very clean code that’s easily extensible.&lt;/p&gt;  &lt;p&gt;Fourthly, &lt;a title="Unity Dependency Injection framework" href="http://www.codeplex.com/unity" target="_blank"&gt;Unity&lt;/a&gt; Dependency Injection (DI) framework is used to pave the path for unit testing and dependency injection. It offers Inversion of Control (IoC), which enables testing individual classes in isolation. Moreover, it has a handy feature to control lifetime of objects. Instead of creating instance of commonly used classes several times within the same request, you can make instances thread level, which means only one instance is created per thread and subsequent calls reuse the same instance. Are these going over your head? No worries, continue reading, I will explain later on.&lt;/p&gt;  &lt;p&gt;Fifthly, enabling API for Silverlight widgets allows more interactive widgets to be built using Silverlight. HTML and Javascripts still have limitations on smooth graphics and continuous transmission of data from web server. Silverlight solves all of these problems.&lt;/p&gt;  &lt;p&gt;Read the article for details on how all these improvements were done and how all these hot techs play together in a very useful open source project for enterprises.&lt;/p&gt;  &lt;p&gt;&lt;a title="Web 2.0 AJAX Portal using jQuery, ASP.NET 3.5, Silverlight, Linq to SQL, WF and Unity" href="http://www.codeproject.com/KB/ajax/Web20Portal.aspx"&gt;http://www.codeproject.com/KB/ajax/Web20Portal.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Don’t forget to vote for me if you like it.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left;margin:0px;padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/omar/archive/2009/04/08/web-2-0-ajax-portal-using-jquery-asp-net-3-5-silverlight-linq-to-sql-wf-and-unity.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/omar/archive/2009/04/08/web-2-0-ajax-portal-using-jquery-asp-net-3-5-silverlight-linq-to-sql-wf-and-unity.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=808080&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1685991" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/omar/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://msmvps.com/blogs/omar/archive/tags/linq/default.aspx">linq</category><category domain="http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx">workflow</category><category domain="http://msmvps.com/blogs/omar/archive/tags/javascript/default.aspx">javascript</category><category domain="http://msmvps.com/blogs/omar/archive/tags/ajax/default.aspx">ajax</category><category domain="http://msmvps.com/blogs/omar/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/omar/archive/tags/silverlight/default.aspx">silverlight</category></item><item><title>Memory Leak with delegates and workflow foundation</title><link>http://msmvps.com/blogs/omar/archive/2009/03/14/memory-leak-with-delegates-and-workflow-foundation.aspx</link><pubDate>Sat, 14 Mar 2009 08:35:53 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1678020</guid><dc:creator>omar</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/omar/rsscomments.aspx?PostID=1678020</wfw:commentRss><comments>http://msmvps.com/blogs/omar/archive/2009/03/14/memory-leak-with-delegates-and-workflow-foundation.aspx#comments</comments><description>&lt;p&gt;Recently after Load Testing my open source project &lt;a href="http://www.dropthings.com" target="_blank"&gt;Dropthings&lt;/a&gt;, I encountered a lot of memory leak. I found lots of Workflow Instances and Linq Entities were left in memory and never collected. After profiling the web application using &lt;a title="Memory Profiler for .NET" href="http://memprofiler.com/" target="_blank"&gt;.NET Memory Profiler&lt;/a&gt;, it showed the real picture:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_41151CF2.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="646" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_416D161B.png" width="605" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It shows you that instances of the several types are being created but not being removed. You see the “New” column has positive value, but the “Remove” column has 0. That means new instances are being created, but not removed. Basically the way you do Memory Profiling is, you take two snapshots. Say you take one snapshot when you first visit your website. Then you do some action on the website that results in allocation of objects. Then you take another snapshot. When you compare both snapshots, you can see how many instances of classes were created between these two snapshots and how many were removed. If they are not equal, then you have leak. Generally in web application many objects are created on every page hit and the end of the request, all those objects are supposed to be released. If they are not released, then we have a problem. But that’s the scenario for desktop applications because in a desktop application, objects can remain in memory until app is closed. But you should know best from the code which objects were supposed to go out of scope and get released.&lt;/p&gt;  &lt;p&gt;For beginners, leak means objects are being allocated but not being freed because someone is holding reference to the objects. When objects leak, they remain in memory forever, until the process (or app domain) is closed. So, if you have a leaky website, your website is continuously taking up memory until it runs out of memory on the web server and thus crash. So, memory leak is a bad – it prevents you from running your product for long duration and requires frequent restart of app pool. &lt;/p&gt;  &lt;p&gt;So, the above screenshot shows Workflow and Linq related classes are not being removed, and thus leaking. This means somewhere workflow instances are not being released and thus all workflow related objects are remaining. You can see the number is same 48 for all workflow related objects. This is a good indication that, almost every instance of workflow is leaked because there were total 48 workflows created and ran. Moreover it indicates we have a leak from a top Workflow instance level, not in some specific Activity or somewhere deep in the code.&lt;/p&gt;  &lt;p&gt;As the workflows use Linq stuff, they held reference to the Linq stuffs and thus the Linq stuffs leaked as well. Sometimes you might be looking for why A is leaking. But you actually end up finding that since B was holding reference to A and B was leaking and thus A was leaking as well. This is sometimes tricky to figure out and you spend a lot of time looking at the wrong direction.&lt;/p&gt;  &lt;p&gt;Now let me show you the buggy code:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ManualWorkflowSchedulerService manualScheduler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;  workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ManualWorkflowSchedulerService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;();

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance instance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;CreateWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Start&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompletedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompletedEventArgs e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 1. instance&lt;/span&gt;
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;; &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 2. terminatedhandler&lt;/span&gt;

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// copy the output parameters in the specified properties dictionary
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Enumerator &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;            e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OutputParameters&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetEnumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;while&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;( &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;MoveNext&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;() )
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;KeyValuePair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Current&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;( &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ContainsKey&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Key&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) )
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;[&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Key&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;] = &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Value&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
            }
        }
    }
};

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Exception &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;x  &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminatedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminatedEventArgs e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 3. instance&lt;/span&gt;
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;; &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 4. completeHandler&lt;/span&gt;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Debug&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WriteLine&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;( &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;x &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
    }
};

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;manualScheduler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;RunWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Can you spot the code where it leaked?&lt;/p&gt;

&lt;p&gt;I have numbered the lines in comment where the leak is happening. Here the &lt;code&gt;delegate&lt;/code&gt; is acting like a closure and those who are from Javascript background know closure is evil. They leak memory unless very carefully written. Here the &lt;code&gt;delegate&lt;/code&gt; keeps a reference to the &lt;code&gt;instance&lt;/code&gt; object. So, if somehow &lt;code&gt;delegate&lt;/code&gt; is not released, the &lt;code&gt;instance&lt;/code&gt; will remain in memory forever and thus leak. Now can you find a situation when the &lt;code&gt;delegate&lt;/code&gt; will not be released?&lt;/p&gt;

&lt;p&gt;Say the workflow completed. It will fire the &lt;font color="#990000"&gt;completeHandler&lt;code&gt;. &lt;/code&gt;&lt;/font&gt;But the &lt;code&gt;completeHandler&lt;/code&gt; will not release the &lt;code&gt;terminateHandler&lt;code&gt;. &lt;/code&gt;&lt;/code&gt;Thus the &lt;code&gt;terminateHandler&lt;/code&gt; remains in memory and it also holds reference to the &lt;code&gt;instance&lt;/code&gt;. So, we have a leaky &lt;code&gt;delegate&lt;/code&gt; leaking whatever it is holding onto outside it’s scope. Here the only thing outside the scope if the &lt;code&gt;instance&lt;/code&gt;, which it is tried to access from the parent function.&lt;/p&gt;

&lt;p&gt;Since the workflow instance is not released, all the properties the workflow and all the activities inside it are holding onto remains in memory. Most of the workflows and activities expose public properties which are Linq Entities. Thus the Linq Entities remain in memory. Now Linq Entities keep a reference to the &lt;code&gt;DataContext&lt;/code&gt; from where it is produced. Thus we have &lt;code&gt;DataContext&lt;/code&gt; remaining in memory. Moreover, &lt;code&gt;DataContext&lt;/code&gt; keeps reference to many internal objects and metadata cacahe, so they remain in memory as well.&lt;/p&gt;

&lt;p&gt;So, the correct code is:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;ManualWorkflowSchedulerService &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;manualScheduler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;ManualWorkflowSchedulerService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;();

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowInstance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;CreateWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Start&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;var &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowCompletedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowCompletedEventArgs &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 1. instanceId is a Guid&lt;/span&gt;
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// copy the output parameters in the specified properties dictionary
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Enumerator &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OutputParameters&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetEnumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;while&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;( &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;MoveNext&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;() )
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;KeyValuePair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;enumerator&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Current&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;( &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ContainsKey&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Key&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) )
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;properties&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;[&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Key&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;] = &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;pair&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Value&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
            }
        }
    }
};

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Exception &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;x  &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowTerminatedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowTerminatedEventArgs &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;) &lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 2. instanceId is a Guid&lt;/span&gt;
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;x &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Debug&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WriteLine&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
    }
};

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;

&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;manualScheduler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;RunWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
&lt;span style="background:#3f3f3f;color:#7f9f7f;"&gt;// 3. Both delegates are now released&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;terminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowRuntime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;completedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There are two changes – in both delegates, the &lt;code&gt;instanceId&lt;/code&gt; variable is passed, instead of the instance. Since &lt;code&gt;instanceId&lt;/code&gt; is a &lt;code&gt;Guid&lt;/code&gt;, which is a struct type data type, not a class, there’s no issue of referencing. Structs are copied, not referenced. So, they don’t leak memory. Secondly, both &lt;code&gt;delegate&lt;/code&gt;s are released at the end of the workflow execution, thus releasing both references.&lt;/p&gt;

&lt;p&gt;In Dropthings, I am using the famous &lt;a title="CallWorkflow Activity" href="http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,7be9fb53-0ddf-4633-b358-01c3e9999088.aspx" target="_blank"&gt;CallWorkflow Activity by John Flanders&lt;/a&gt;, which is widely used to execute one Workflow from another synchronously. There’s a &lt;code&gt;CallWorkflowService&lt;/code&gt; class which is responsible for synchronously executing another workflow and that has similar memory leak problem. The original code of the service is as following:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public class &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;CallWorkflowService &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;: &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowRuntimeService
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#region &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Methods

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public void &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;StartWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Type &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;inparms&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;           Guid &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;IComparable &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowRuntime wr &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance wi &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;CreateWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;inparms&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Start&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ManualWorkflowSchedulerService ss &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;             &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ManualWorkflowSchedulerService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;();
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ss &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;!= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ss&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;RunWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompletedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;d  &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;d &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompletedEventArgs e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;==&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;d&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance c &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;EnqueueItem&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OutputParameters&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
            }
        };
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminatedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;te &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;te &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminatedEventArgs e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;-= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;te&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance c &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;EnqueueItem&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;new &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#c89191;"&gt;&amp;quot;Called Workflow Terminated&amp;quot;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;br /&gt;                  &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;), &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
            }
        };
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;d&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;te&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
    }

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#endregion &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Methods
}&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you see, it has that same delegate holding reference to instance object problem. Moreover, there’s some queue stuff there, which requires the &lt;code&gt;caller&lt;/code&gt; and &lt;code&gt;qn&lt;/code&gt; parameter passed to the &lt;code&gt;StartWorkflow&lt;/code&gt; function. So, not a straight forward fix. &lt;/p&gt;

&lt;p&gt;I tried to rewrite the whole &lt;code&gt;CallWorkflowService&lt;/code&gt; so that it does not require two delegates to be created per Workflow. Then I took the delegates out. Thus there’s no chance of closure holding reference to unwanted objects. The result looks like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public class &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;CallWorkflowService &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;: &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowRuntimeService
&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;{
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#region &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Fields

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;private &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowCompletedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_CompletedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;private &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;EventHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowTerminatedEventArgs&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_TerminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;private &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Guid&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;       new &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Guid&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;();

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#endregion &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Fields

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#region &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Methods

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public void &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;StartWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Type &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Dictionary&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;string&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt; &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;inparms&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;br /&gt;        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Guid &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;IComparable &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowRuntime &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowInstance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;CreateWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;workflowType&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;,&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;inparms&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Start&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;var &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;[&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;] = &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;new &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;{ &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Caller &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;};

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;ManualWorkflowSchedulerService &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ss &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;           wr&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;ManualWorkflowSchedulerService&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;&amp;gt;();
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ss &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;!= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ss&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;RunWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wi&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
    }

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;protected override void &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OnStarted&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;()
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;base&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OnStarted&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_CompletedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_CompletedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowCompletedEventArgs &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;var &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ContainsKey&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;))
                {
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;[&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;];
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowInstance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;EnqueueItem&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OutputParameters&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Remove&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                }
            };
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowCompleted &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_CompletedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        }

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;== &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_TerminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
        {
            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_TerminatedHandler &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;delegate&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;object &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;o&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowTerminatedEventArgs &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;)
            {
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;var &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowInstance&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;InstanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
                &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;if &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;ContainsKey&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;))
                {
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;[&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;];
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;WorkflowInstance &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;GetWorkflow&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;c&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;EnqueueItem&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;wf&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;                      new &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#f0dfaf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#c89191;"&gt;&amp;quot;Called Workflow Terminated&amp;quot;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;e&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Exception&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;), &lt;br /&gt;&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;                      null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;, &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;null&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Remove&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;(&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;instanceId&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;);
                }
            };

            &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;this&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Runtime&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;WorkflowTerminated &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;+= &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_TerminatedHandler&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        }
    }

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;protected override void &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OnStopped&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;()
    {
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;_WorkflowQueue&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Clear&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;base&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;.&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;OnStopped&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;();
    }

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#endregion &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Methods

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#region &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Nested Types

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;private struct &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;WorkflowInfo
    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;{
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#region &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Fields

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;Guid &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;Caller&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;
        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#eaeaac;"&gt;public &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#2b91af;"&gt;IComparable &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfdfbf;"&gt;qn&lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;;

        &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#endregion &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Fields
    }

    &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dfaf8f;"&gt;#endregion &lt;/span&gt;&lt;span style="background:#3f3f3f;color:#dcdccc;"&gt;Nested Types
}&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;After fixing the problem, another Memory Profile result showed the leak is gone:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_0CF441A2.png"&gt;&lt;img title="image" style="border-top-width:0px;display:inline;border-left-width:0px;border-bottom-width:0px;border-right-width:0px;" height="601" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_2BCA8B51.png" width="564" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you see, the numbers vary, which means there’s no consistent leak. Moreover, looking at the types that remains in memory, they look more like metadata than instances of classes.&amp;#160; So, they are basically cached instances of metadata, not instances allocated during workflow execution which are supposed to be freed. So, we solved the memory leak!&lt;/p&gt;

&lt;p&gt;Now you know how to write anonymous delegates without leaking memory and how to run workflow without leaking them. Basically, the principle theory is – if you are referencing some outside object from an anonymous &lt;code&gt;delegate&lt;/code&gt;, make sure that object is not holding reference to the &lt;code&gt;delegate&lt;/code&gt; in some way, may be directly or may be via some child objects of its own. Because then you have a circular reference. If possible, do not try to access objects e.g. &lt;code&gt;instance&lt;/code&gt; inside an anonymous delegate that is declared outside the delegate. Try accessing instrinsic data types like int, string, DateTime, Guid etc which are not reference type variables. So, instead of referencing to an object, you should declare local variables e.g. &lt;code&gt;instanceId&lt;/code&gt; that gets the value of properties (e.g. &lt;code&gt;instance.InstanceId&lt;/code&gt;) from the object and then use those local variables inside the anonymous delegate.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left;margin:0px;padding:4px 4px 4px 4px;"&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://msmvps.com/blogs/omar/archive/2009/03/14/memory-leak-with-delegates-and-workflow-foundation.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://msmvps.com/blogs/omar/archive/2009/03/14/memory-leak-with-delegates-and-workflow-foundation.aspx&amp;amp;bgcolor=0080C0&amp;amp;fgcolor=FFFFFF&amp;amp;border=000000&amp;amp;cbgcolor=808080&amp;amp;cfgcolor=000000" alt="DotNetKicks Image" border="0/" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1678020" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/omar/archive/tags/performance/default.aspx">performance</category><category domain="http://msmvps.com/blogs/omar/archive/tags/linq/default.aspx">linq</category><category domain="http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx">workflow</category><category domain="http://msmvps.com/blogs/omar/archive/tags/.net/default.aspx">.net</category></item><item><title>Strongly typed workflow input and output arguments</title><link>http://msmvps.com/blogs/omar/archive/2008/12/27/strongly-typed-workflow-input-and-output-arguments.aspx</link><pubDate>Sat, 27 Dec 2008 15:05:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1657833</guid><dc:creator>omar</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/omar/rsscomments.aspx?PostID=1657833</wfw:commentRss><comments>http://msmvps.com/blogs/omar/archive/2008/12/27/strongly-typed-workflow-input-and-output-arguments.aspx#comments</comments><description>&lt;p&gt;When you run a &lt;u&gt;Workflow&lt;/u&gt; using Workflow Foundation, you pass arguments to the workflow in a &lt;u&gt;Dictionary&lt;/u&gt; form where the type of Dictionary is &lt;u&gt;Dictionary&amp;lt;string, object&amp;gt;&lt;/u&gt;. This means you miss the strong typing features of .NET languages. You have to know what arguments the workflow expects by looking at the &lt;u&gt;Workflow&lt;/u&gt; public properties. Moreover, there’s no way to make arguments required. You pass parameter, expect it to run, if it throws exception, you pass more arguments, hope it works now. Similarly, if you are running workflow synchronously using &lt;u&gt;ManualWorkflowSchedulerService&lt;/u&gt;, you expect return arguments from the Workflow immediately, but there again, you have to rely on the &lt;u&gt;Dictionary&lt;/u&gt; key and value pair. No strong typing there as well.&lt;/p&gt;  &lt;p&gt;In order to solve this, so that you could pass Workflow arguments as strongly typed classes, you can establish a format that every &lt;u&gt;Workflow&lt;/u&gt; has only two arguments named &amp;quot;Request” and “Response” and none other. Whatever needs to be passed to the &lt;u&gt;Workflow&lt;/u&gt; and expected out of it, must be passed via Request and must be expected via Response properties. Now the type of these arguments can be workflow specific, it can be any class with one or more parameters. This way, you could write code like this:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_0C53CAC7.png"&gt;&lt;img title="Running workflow with strongly typed argument" style="border-right:0px;border-top:0px;display:inline;border-left:0px;border-bottom:0px;" height="100" alt="Running workflow with strongly typed argument" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_0166C3B2.png" width="657" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The advantages of these strongly typed approach are: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Compile time validation of input parameters passed to workflow. No risk of passing unexpected object in Dictionary’s &lt;u&gt;object&lt;/u&gt; type value.&lt;/li&gt;    &lt;li&gt;Enforce required values by creating Request objects with non-default constructor.&lt;/li&gt;    &lt;li&gt;Establish a fixed contract for Workflow input and output via the strongly typed Request and Response classes or interfaces.&lt;/li&gt;    &lt;li&gt;Validate input arguments for the Workflow directly from the Request class, without going through the overhead of running a workflow.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If we follow this approach, we create workflows with only two &lt;u&gt;DependencyProperty&lt;/u&gt;, one for &lt;u&gt;Request&lt;/u&gt; and one for &lt;u&gt;Response&lt;/u&gt;. Showing you an example from my open source project &lt;a title="Dropthings - Open Source AJAX Start Page" href="http://www.codeplex.com/dropthings" target="_blank"&gt;Dropthings&lt;/a&gt;, which uses Workflow for the entire Business Layer. Below you see the Workflow that executes when a new user visits &lt;a title="Dropthings - an Open Source AJAX Start Page" href="http://www.dropthings.com" target="_blank"&gt;Dropthings.com&lt;/a&gt;, creates a new user and setups all the pages and widgets for the user. It has only two &lt;u&gt;Dependency&lt;/u&gt; property – Request and Response.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_4C29C31A.png"&gt;&lt;img title="image" style="display:inline;" height="547" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_1A4E5A2B.png" width="595" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;The Request parameters is of type &lt;u&gt;IUserVisitWorkflowRequest&lt;/u&gt;. So, you can pass any class as Request argument that implements the interface. &lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_65BD6215.png"&gt;&lt;img title="image" style="display:inline;" height="69" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_152BD0E2.png" width="637" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Here I have used fancy inheritance to create Request object hierarchy. You don’t need to do that. Just remember, you can pass any class. You don’t even need to use interface for Request parameter. It can be a class directly. I use all these interfaces in order to facilitate Dependency Inversion.&lt;/p&gt;  &lt;p&gt;Similarly, the Response object is also a class.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_366B2AE4.png"&gt;&lt;img title="image" style="display:inline;" height="117" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_23F2B462.png" width="656" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The Response returns quite some properties. So, it’s kinda handy to wrap them all in one property.&lt;/p&gt;  &lt;p&gt;So, there you have it, strongly typed Workflow arguments. You can attach properties of the Request object to any activity directly form the designer:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_6B37E487.png"&gt;&lt;img title="image" style="display:inline;" height="520" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_2B2E2875.png" width="534" border="0" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;There’s really no compromise to make in this approach. Everything works as before.&lt;/p&gt;  &lt;p&gt;In order to make workflow execution simpler, I use a helper method like the following, that takes the Request and Response object and creates the Dictionary for me. This Dictionary always contains one “Request” and one “Response” entry.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_066CEB31.png"&gt;&lt;img title="image" style="display:inline;" height="196" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_6A7BB638.png" width="498" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;This way, I can run Workflow in strongly typed fashion:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_6DA8C87A.png"&gt;&lt;img title="image" style="display:inline;" height="144" alt="image" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/omar/image_5F00_thumb_5F00_3DFA8421.png" width="646" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Here I can specify the Request, Response and Workflow type using strong typing. This way I get strongly typed return object as well as pass strongly type Request object. There’s no dictionary building, no risky string key and object type value passing.&amp;#160; You can ignore the &lt;u&gt;ObjectContainer.Resolve()&lt;/u&gt; stuff, because that’s just returning me an existing reference of &lt;u&gt;WorkflowRuntime&lt;/u&gt;.&lt;/p&gt;  &lt;p&gt;Hope you like this approach.&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:left;margin:0px;padding:4px 4px 4px 4px;"&gt;&lt;script type="text/javascript"&gt;var dzone_url = &amp;#39;http://msmvps.com/blogs/omar/archive/2008/12/27/strongly-typed-workflow-input-and-output-arguments.aspx&amp;#39;;&lt;/script&gt;&lt;script type="text/javascript"&gt;var dzone_title = &amp;#39;Strongly typed workflow input and output arguments&amp;#39;;&lt;/script&gt;&lt;script type="text/javascript"&gt;var dzone_blurb = &amp;#39;Strongly typed workflow input and output arguments&amp;#39;;&lt;/script&gt;&lt;script type="text/javascript"&gt;var dzone_style = &amp;#39;2&amp;#39;;&lt;/script&gt;&lt;script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"&gt;&lt;/script&gt; &lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1657833" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx">workflow</category><category domain="http://msmvps.com/blogs/omar/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/omar/archive/tags/architecture/default.aspx">architecture</category></item><item><title>My first book - Building a Web 2.0 Portal with ASP.NET 3.5</title><link>http://msmvps.com/blogs/omar/archive/2008/01/13/my-first-book-building-a-web-2-0-portal-with-asp-net-3-5.aspx</link><pubDate>Sun, 13 Jan 2008 20:23:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1461326</guid><dc:creator>omar</dc:creator><slash:comments>44</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/omar/rsscomments.aspx?PostID=1461326</wfw:commentRss><comments>http://msmvps.com/blogs/omar/archive/2008/01/13/my-first-book-building-a-web-2-0-portal-with-asp-net-3-5.aspx#comments</comments><description>&lt;p&gt;My first book &amp;quot;&lt;a href="http://www.oreilly.com/catalog/9780596510503/" target="_blank"&gt;Building a Web 2.0 Portal with ASP.NET 3.5&lt;/a&gt;&amp;quot; from O&amp;#39;Reilly is published and available in the stores. This book explains in detail the architecture design, development, test, deployment, performance and scalability challenges of my open source web portal &lt;a href="http://www.dropthings.com" target="_blank"&gt;Dropthings.com&lt;/a&gt;. Dropthings is a prototype of a web portal similar to &lt;a href="http://www.google.com/ig" target="_blank"&gt;iGoogle&lt;/a&gt; or &lt;a href="http://www.pageflakes.com" target="_blank"&gt;Pageflakes&lt;/a&gt;. But this portal is developed using recently released brand new technologies like ASP.NET 3.5, C# 3.0, Linq to Sql, Linq to XML, and Windows Workflow foundation. It makes heavy use of ASP.NET AJAX 1.0. Throughout my career I have built several state-of-the-art &lt;a href="http://omar.mvps.org" target="_blank"&gt;personal&lt;/a&gt;, educational, enterprise and &lt;a href="http://www.pageflakes.com" target="_blank"&gt;mass consumer web portals&lt;/a&gt;. This book collects my experience in building all of those portals.&lt;/p&gt;&lt;p&gt;O&amp;#39;Reilly Website:&lt;br /&gt;&lt;a href="http://www.oreilly.com/catalog/9780596510503/"&gt;http://www.oreilly.com/catalog/9780596510503/&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Amazon:&lt;br /&gt;&lt;a href="http://www.amazon.com/Building-Web-2-0-Portal-ASP-NET/dp/0596510500"&gt;http://www.amazon.com/Building-Web-2-0-Portal-ASP-NET/dp/0596510500&lt;/a&gt;&lt;br /&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Disclaimer: This book does not show you how to build Pageflakes. Dropthings is entirely different in terms of architecture, implementation and the technologies involved.&lt;/p&gt; &lt;p&gt;You learn how to: &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Implement a highly decoupled architecture following the popular n-tier, widget-based application model  &lt;/li&gt;&lt;li&gt;Provide drag-and-drop functionality, and use ASP.NET 3.5 to build the server-side part of the web layer  &lt;/li&gt;&lt;li&gt;Use LINQ to build the data access layer, and Windows Workflow Foundation to build the business layer as a collection of workflows  &lt;/li&gt;&lt;li&gt;Build client-side widgets using JavaScript for faster performance and better caching  &lt;/li&gt;&lt;li&gt;Get maximum performance out of the ASP.NET AJAX Framework for faster, more dynamic, and scalable sites  &lt;/li&gt;&lt;li&gt;Build a custom web service call handler to overcome shortcomings in ASP.NET AJAX 1.0 for asynchronous, transactional, cache-friendly web services  &lt;/li&gt;&lt;li&gt;Overcome JavaScript performance problems, and help the user interface load faster and be more responsive  &lt;/li&gt;&lt;li&gt;Solve various scalability and security problems as your site grows from hundreds to millions of users  &lt;/li&gt;&lt;li&gt;Deploy and run a high-volume production site while solving software, hardware, hosting, and Internet infrastructure problems &lt;/li&gt;&lt;/ul&gt;If you&amp;#39;re ready to build state-of-the art, high-volume web applications that can withstand millions of hits per day, this book has exactly what you need.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1461326" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/omar/archive/tags/sql+server/default.aspx">sql server</category><category domain="http://msmvps.com/blogs/omar/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://msmvps.com/blogs/omar/archive/tags/pageflakes/default.aspx">pageflakes</category><category domain="http://msmvps.com/blogs/omar/archive/tags/production/default.aspx">production</category><category domain="http://msmvps.com/blogs/omar/archive/tags/linq/default.aspx">linq</category><category domain="http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx">workflow</category><category domain="http://msmvps.com/blogs/omar/archive/tags/ajax/default.aspx">ajax</category><category domain="http://msmvps.com/blogs/omar/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/omar/archive/tags/IIS/default.aspx">IIS</category></item><item><title>Executing one workflow from another synchronously</title><link>http://msmvps.com/blogs/omar/archive/2006/12/27/executing-one-workflow-from-another-synchronously.aspx</link><pubDate>Wed, 27 Dec 2006 15:38:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:454049</guid><dc:creator>omar</dc:creator><slash:comments>114</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/omar/rsscomments.aspx?PostID=454049</wfw:commentRss><comments>http://msmvps.com/blogs/omar/archive/2006/12/27/executing-one-workflow-from-another-synchronously.aspx#comments</comments><description>&lt;p&gt;The 
&lt;code&gt;InvokeWorkflow&lt;/code&gt; activity which comes with Workflow
Foundation (.NET 3.0)&amp;nbsp;executes a workflow asynchronously. So,
if you are calling a workflow from ASP.NET which in turn calls
another workflow, the second workflow is going to be terminated
prematurely instead of executing completely. The reason is, 
&lt;code&gt;ManualWorkflowSchedulerService&lt;/code&gt; will execute the first
workflow synchronously and then finish the workflow execution and
close down. If you used 
&lt;code&gt;InvokeWorkflow&lt;/code&gt; activity in order to run another
workflow from the first workflow, it will start on another thread
and it will not get enough time to execute completely before the
parent workflow ends.&lt;/p&gt;
&lt;p&gt;
&lt;img height="163" src="http://omar.mvps.org/images/Executingoneworkflowfromanothersynchrono_13049/AsyncExecution1.png" width="580" alt="" /&gt;
&lt;/p&gt;
&lt;p&gt;Here you see only one activity in the second workflow gets the
chance to execute. The remaining two activities do not get called
at all.&lt;/p&gt;
&lt;p&gt;Luckily I found an implementation of synchronous workflow
execution at:&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,7be9fb53-0ddf-4633-b358-01c3e9999088.aspx"&gt;
http://www.masteringbiztalk.com/blogs/jon/PermaLink,guid,7be9fb53-0ddf-4633-b358-01c3e9999088.aspx&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;It&amp;#39;s an activity which takes the workflow as input and executes
it synchronously. The implementation is very interesting to see.
Please see the original blog post for details.&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=454049" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/omar/archive/tags/workflow/default.aspx">workflow</category></item></channel></rss>