title: Windows Workflow 4 Correlation Query includes website instance name in instance key calculation and fails
body: <p>I am trying to host a long running workflow service on Azure but I am having problems with correlation.<br>
  I have got the timeToUnload and the timeToPersist set to 0 and I have ticked the "persist before send" in the worklow - this is not a problem with persistence, it is to do with how instance keys are calculated.</p>
  
  <p>When one web server starts a workflow and another then tries to take another action on the workflow, it fails with </p>
  
  <blockquote>
    <p>System.ServiceModel.FaultException: The execution of an InstancePersistenceCommand was
        interrupted because the instance key '12e0b449-7a71-812d-977a-ab89864a272f' was not 
        associated to an instance. This can occur because the instance or key has been cleaned up, 
        or because the key is invalid. The key may be invalid if the message it was generated from 
        was sent at the wrong time or contained incorrect correlation data.</p>
  </blockquote>
  
  <p>I used the wcf service diagnostic to dig into this and I found that it is because the calculation of the instance key includes the <em>website instance name</em>, thus a given workflow instance can only be called back from the same machine that instantiated it (<em>because Azure set a different website instance name on each role instance</em>).</p>
  
  <p>To explain, when I create a new instance of the workflow, I have an activity that gets the workflow instance Guid and then returns that guid and also uses the correlation initializer to set the correlation handle.</p>
  
  <p>I have enabled Service Tracing in web.config so in the Service Trace Viewer I can see the following happening when I instantiate a new instance of the workflow;</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;ApplicationData &gt;
      &lt;TraceData &gt;
          &lt;DataItem &gt;
              &lt;TraceRecord Severity ="Information" Channel="Analytic " xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord "&gt;
                  &lt;TraceIdentifier &gt;225&lt;/ TraceIdentifier&gt;
                  &lt;Description &gt;Calculated correlation key '496e3207-fe9d-919f-b1df-f329c5a64934' using values 'key1:10013d62-286e-4a8f-aeb2-70582591cd7f,' in parent scope '{/NewOrbit.ExVerifier.Web_IN_2_Web/Workflow/Application/}Application_default1.xamlx'.&lt;/Description &gt;
                  &lt;AppDomain &gt;/LM/W3SVC/1273337584/ROOT-1-129811251826070757&lt;/AppDomain &gt;
              &lt;/TraceRecord &gt;
          &lt;/DataItem &gt;
      &lt;/TraceData &gt;
  &lt;/ApplicationData &gt;
  </code></pre>
  
  <p>The important line is this:</p>
  
  <blockquote>
    <p>Calculated correlation key '496e3207-fe9d-919f-b1df-f329c5a64934' using values 'key1:10013d62-286e-4a8f-aeb2-70582591cd7f,' in parent scope '{/NewOrbit.ExVerifier.Web_IN_2_Web/Workflow/Application/}Application_default1.xamlx'.</p>
  </blockquote>
  
  <p>The Guid of this particular workflow instance is <code>10013d62-286e-4a8f-aeb2-70582591cd7f</code> so the workflow engine calculates an "instance key" from that which is <code>496e3207-fe9d-919f-b1df-f329c5a64934</code>.
  I can see the workflow instance with the guid in <code>[System.Activities.DurableInstancing].[InstancesTable]</code> and I can see the instance key in <code>[System.Activities.DurableInstancing].[KeysTable]</code>.
  So far, so good and if the same server makes a later call to that same workflow, everything works fine. However, if a <em>different</em> server tries to access the workflow, I get the correlation error mentioned above. Once again looking at the diagnostics trace, I can see this: </p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;TraceData &gt;
      &lt;DataItem &gt;
          &lt;TraceRecord Severity ="Information" Channel="Analytic " xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord "&gt;
              &lt;TraceIdentifier &gt;225&lt;/ TraceIdentifier&gt;
              &lt;Description &gt;Calculated correlation key '12e0b449-7a71-812d-977a-ab89864a272f' using values 'key1:10013d62-286e-4a8f-aeb2-70582591cd7f,' in parent scope '{/NewOrbit.ExVerifier.Web_IN_5_Web/Workflow/Application/}Application_default1.xamlx'.                     &lt;/Description &gt;
              &lt;AppDomain &gt;/LM/W3SVC/1273337584/ROOT-1-129811251818669004&lt;/AppDomain &gt;
          &lt;/TraceRecord &gt;
      &lt;/DataItem &gt;
  &lt;/TraceData &gt;
  </code></pre>
  
  <p>The important line is</p>
  
  <blockquote>
    <p>Calculated correlation key '12e0b449-7a71-812d-977a-ab89864a272f' using values 'key1:10013d62-286e-4a8f-aeb2-70582591cd7f,' in parent scope '{/NewOrbit.ExVerifier.Web_IN_5_Web/Workflow/Application/}Application_default1.xamlx'. </p>
  </blockquote>
  
  <p>As you can see, it is the same Guid being passed in but the system includes the name of the website instance in the calculation of the Instance key so it ends up with a completely different instance key.</p>
  
  <p>I have created a completely new project to test this out and found the exact same problem. I feel I must be doing something very simple wrong as I can't find anyone else with the same problem.</p>