title: Dynamics CRM 2011 InvalidWorkflowException
body: <p>I have 2 custom entities and a list of Calendar rules which i'm trying to each parse to a separate list of a 3rd custom entity, 1 or 2 for each day. I then Union those lists together to make a final list which contains the right entities.</p>
  
  <p>I'm now trying to debug the workflow before I finalize it by generating all the entities, but I have to fix several processing errors first.</p>
  
  <p>Errors I get:</p>
  
  <pre><code>Unhandled Exception: System.Activities.InvalidWorkflowException: The following errors were encountered while processing the workflow tree:
  'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "DirectCast(CustomActivityStep1: tijdindelingen maken_1_converted, System.String)".
  Syntax error in cast operator; two arguments separated by comma are required.
  
  'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "DirectCast(CustomActivityStep1_1_converted, Microsoft.Xrm.Sdk.EntityReference)".Invalid L-value expression.:Reference expressions cannot end with Conversion. The provided expression's type must exactly match the type T of VisualBasicReference&amp;lt;T&amp;gt; or LambdaReference&amp;lt;T&amp;gt;.
  'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "CustomActivityStep1: tijdindelingen maken_1_converted".
  End of expression expected.
  
  'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "CustomActivityStep1: tijdindelingen maken_1".
  End of expression expected.
  
  'DynamicActivity': The private implementation of activity '1: DynamicActivity' has the following validation error:   Compiler error(s) encountered processing expression "CustomActivityStep1: tijdindelingen maken_1".
  End of expression expected.
  
     at System.Activities.Hosting.WorkflowInstance.ValidateWorkflow(WorkflowInstanceExtensionManager extensionManager)
     at System.Activities.WorkflowApplication.EnsureInitialized()
     at System.Activities.WorkflowApplication.Enqueue(InstanceOperation operation, Boolean push)
     at System.Activities.WorkflowApplication.WaitForTurn(InstanceOperation operation, TimeSpan timeout)
     at System.Activities.WorkflowApplication.InternalRun(TimeSpan timeout, Boolean isUserRun)
     at Microsoft.Crm.Workflow.SynchronousRuntime.ActivityHost.StartWorkflowExecution(Activity workflow, ICommonWorkflowContext context)
     at Microsoft.Crm.Workflow.SynchronousRuntime.ActivityHost.StartWorkflow(ICommonWorkflowContext context)
  </code></pre>
  
  <p>Code which gives these errors:</p>
  
  <pre><code>// &lt;copyright file="GenerateTijdIndelingen.cs" company=""&gt;
  // Copyright (c) 2013 All Rights Reserved
  // &lt;/copyright&gt;
  // &lt;author&gt;&lt;/author&gt;
  // &lt;date&gt;11/22/2013 12:28:26 PM&lt;/date&gt;
  // &lt;summary&gt;Implements the GenerateTijdIndelingen Workflow Activity.&lt;/summary&gt;
  
  using System.Collections.Generic;
  using System.Globalization;
  using System.Linq;
  using Microsoft.Xrm.Sdk.Messages;
  using Microsoft.Xrm.Sdk.Metadata;
  
  namespace acm.mscrm2011.hrpro.Workflow
  {
      using System;
      using System.Activities;
      using System.ServiceModel;
      using Microsoft.Xrm.Sdk;
      using Microsoft.Xrm.Sdk.Workflow;
  
      public sealed class GenerateTijdIndelingen : CodeActivity
      {
          [Input("Personeelsfiche")]
          [ReferenceTarget(slfn_personeelsfiche.EntityLogicalName)]
          public InArgument&lt;EntityReference&gt; Personeelsfichereference { get; set; }
          /// &lt;summary&gt;
          /// Executes the workflow activity.
          /// &lt;/summary&gt;
          /// &lt;param name="executionContext"&gt;The execution context.&lt;/param&gt;
          protected override void Execute(CodeActivityContext executionContext)
          {
  
  
              // Create the tracing service
              ITracingService tracingService = executionContext.GetExtension&lt;ITracingService&gt;();
  
              if (tracingService == null)
              {
                  throw new InvalidPluginExecutionException("Failed to retrieve tracing service.");
              }
  
              tracingService.Trace("Entered GenerateTijdIndelingen.Execute(), Activity Instance Id: {0}, Workflow Instance Id: {1}",
                  executionContext.ActivityInstanceId,
                  executionContext.WorkflowInstanceId);
  
              // Create the context
              IWorkflowContext context = executionContext.GetExtension&lt;IWorkflowContext&gt;();
  
              if (context == null)
              {
                  throw new InvalidPluginExecutionException("Failed to retrieve workflow context.");
              }
  
              tracingService.Trace("GenerateTijdIndelingen.Execute(), Correlation Id: {0}, Initiating User: {1}",
                  context.CorrelationId,
                  context.InitiatingUserId);
              IOrganizationServiceFactory serviceFactory = executionContext.GetExtension&lt;IOrganizationServiceFactory&gt;();
              // This is where the magic happens          
              Type type = Type.GetType("Microsoft.Crm.Workflow.SynchronousRuntime.WorkflowContext, Microsoft.Crm.Workflow, Version=5.0.0.0");
              type.GetProperty("ProxyTypesAssembly").SetValue(serviceFactory, typeof(XrmServiceContext).Assembly, null);
              IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
              XrmServiceContext serviceContext = new XrmServiceContext(service);
  
              try
              {
                  //gegevens ophalen
                  Guid personeelslidGuid = Personeelsfichereference.Get&lt;slfn_personeelsfiche&gt;(executionContext).Id;
                  IEnumerable&lt;CalendarRule&gt; applicableCalendarRules = GetCalendarRules(serviceContext).Where(cr =&gt; IsInNMonthsOrLess(cr.StartTime.GetValueOrDefault(), 2));
                  Guid feestDagGuid = GetAfwezigheidsRedenId(serviceContext, "Feestdag");
                  IEnumerable&lt;acm_weekkalender&gt; weekkalenders = GetWeekKalender(serviceContext, personeelslidGuid);
                  IEnumerable&lt;acm_afwezigheden&gt; applicableAfwezigheden = GetAfwezighedenForPersoneelslid(serviceContext, personeelslidGuid).Where(afw =&gt; IsInNMonthsOrLess(afw.acm_eerstedag.GetValueOrDefault(), 2) || IsInNMonthsOrLess(afw.acm_laatstedag.GetValueOrDefault(), 2));
                  List&lt;acm_tijdindeling&gt; feestdagTijdindelingen = new List&lt;acm_tijdindeling&gt;();
  
                  //calendarrules parsen
                  foreach (CalendarRule calendarRule in applicableCalendarRules)
                  {
                      acm_tijdindeling feestdagtijdindeling = new acm_tijdindeling { acm_personeelsfiche = new EntityReference(slfn_personeelsfiche.EntityLogicalName, personeelslidGuid), acm_starttijdstip = calendarRule.StartTime, acm_eindtijdstip = calendarRule.EndTime.GetValueOrDefault(), acm_iswerktijd = false, acm_redenvanafwezigheid = new EntityReference(acm_afwezigheidsreden.EntityLogicalName, feestDagGuid) };
                      feestdagtijdindeling.acm_name = String.Format(feestdagtijdindeling.acm_personeelsfiche.Name + " - " + feestdagtijdindeling.acm_redenvanafwezigheid.Name + " - " + feestdagtijdindeling.acm_starttijdstip.GetValueOrDefault().ToShortDateString());
                      feestdagTijdindelingen.Add(feestdagtijdindeling);
                  }
                  //afwezigheden parsen
                  List&lt;acm_tijdindeling&gt; afwezigheidTijdIndelingen = new List&lt;acm_tijdindeling&gt;();
                  foreach (acm_afwezigheden acmAfwezigheid in applicableAfwezigheden)
                  {
                      acm_tijdindeling afwezigheidtijdindeling = new acm_tijdindeling { acm_personeelsfiche = new EntityReference(slfn_personeelsfiche.EntityLogicalName, acmAfwezigheid.acm_personeelslid.Id), acm_starttijdstip = acmAfwezigheid.acm_eerstedag.GetValueOrDefault(), acm_eindtijdstip = acmAfwezigheid.acm_laatstedag.GetValueOrDefault(), acm_iswerktijd = false, acm_redenvanafwezigheid = new EntityReference(acm_afwezigheidsreden.EntityLogicalName, acmAfwezigheid.acm_afwezigheidsreden.Id) };
                      afwezigheidtijdindeling.acm_name = String.Format(afwezigheidtijdindeling.acm_personeelsfiche.Name + " - " + afwezigheidtijdindeling.acm_redenvanafwezigheid.Name + " - " + afwezigheidtijdindeling.acm_starttijdstip.GetValueOrDefault().ToShortDateString());
                      afwezigheidTijdIndelingen.Add(afwezigheidtijdindeling);
                  }
                  //weekkalenders parsen
                  List&lt;acm_tijdindeling&gt; werktijdIndelingen = new List&lt;acm_tijdindeling&gt;();
                  foreach (acm_weekkalender acmWeekkalender in weekkalenders)
                  {
                      DateTime contractstartDateTime = DateTime.Now;
                      DateTime contractendDateTime = contractstartDateTime.AddMonths(2);
                      List&lt;DayOfWeek&gt; dayOfWeeks = Enum.GetValues(typeof(DayOfWeek)).Cast&lt;DayOfWeek&gt;().ToList();
                      CultureInfo dutchCultureInfo = new CultureInfo("nl-BE");
                      Array vmnmList = Enum.GetValues(typeof(DagDeel));
                      foreach (DayOfWeek dayOfWeek in dayOfWeeks)
                      {
                          // The (... + 7) % 7 ensures we end up with a value in the range [0, 6]
                          int daysUntilNextDay = ((int)dayOfWeek - (int)contractstartDateTime.DayOfWeek + 7) % 7;
                          DateTime nextDateTime = contractstartDateTime.AddDays(daysUntilNextDay).AddDays((acmWeekkalender.acm_volgnummer.GetValueOrDefault(1) - 1) * 7);
                          string weekdag = dutchCultureInfo.DateTimeFormat.DayNames[(int)dayOfWeek].ToLower();
                          foreach (DagDeel dagDeel in vmnmList)
                          {
                              string lowerDagDeel = dagDeel.ToString().ToLower();
                              if (acmWeekkalender.GetAttributeValue&lt;Boolean&gt;("acm_" + weekdag + lowerDagDeel + "vrijaf")) continue;
                              int startuur = acmWeekkalender.GetAttributeValue&lt;OptionSetValue&gt;("acm_" + weekdag + lowerDagDeel + "startuur").Value;
                              int startminuut = acmWeekkalender.GetAttributeValue&lt;OptionSetValue&gt;("acm_" + weekdag + lowerDagDeel + "startminuut").Value;
                              int einduur = acmWeekkalender.GetAttributeValue&lt;OptionSetValue&gt;("acm_" + weekdag + lowerDagDeel + "einduur").Value;
                              int eindminuut = acmWeekkalender.GetAttributeValue&lt;OptionSetValue&gt;("acm_" + weekdag + lowerDagDeel + "eindminuut").Value;
  
                              DateTime startDateTime = new DateTime(nextDateTime.Year, nextDateTime.Month, nextDateTime.Day, Convert.ToInt16(GetOptionsetText("acm_uren", startuur, service)), Convert.ToInt16(GetOptionsetText("acm_minuten", startminuut, service)), 0, DateTimeKind.Local);
                              DateTime endDateTime = new DateTime(nextDateTime.Year, nextDateTime.Month, nextDateTime.Day, Convert.ToInt16(GetOptionsetText("acm_uren", einduur, service)), Convert.ToInt16(GetOptionsetText("acm_minuten", eindminuut, service)), 0, DateTimeKind.Local);
                              for (DateTime dateTime = nextDateTime; dateTime &lt; contractendDateTime; dateTime.AddDays(7))
                              {
                                  acm_tijdindeling werktijdindeling = new acm_tijdindeling { acm_personeelsfiche = new EntityReference(slfn_personeelsfiche.EntityLogicalName, acmWeekkalender.acm_werknemer.Id), acm_weekkalender = new EntityReference(acm_weekkalender.EntityLogicalName, acmWeekkalender.Id), acm_starttijdstip = startDateTime, acm_eindtijdstip = endDateTime, acm_iswerktijd = true };
                                  werktijdindeling.acm_name = String.Format(werktijdindeling.acm_personeelsfiche.Name + " - " + werktijdindeling.acm_redenvanafwezigheid.Name + " - " + werktijdindeling.acm_starttijdstip.GetValueOrDefault().ToShortDateString());
                                  werktijdIndelingen.Add(werktijdindeling);
                              }
                          }
                      }
                  }
                  //debug info.
                  List&lt;acm_tijdindeling&gt; unionTijdindelingen = feestdagTijdindelingen.Union(afwezigheidTijdIndelingen, new TijdIndelingComparer()).Union(werktijdIndelingen, new TijdIndelingComparer()).ToList();
                  int aantalFeestdagen = unionTijdindelingen.Count(uti =&gt; uti.acm_redenvanafwezigheid.Id.Equals(feestDagGuid));
                  int aantalwerkdagen = unionTijdindelingen.Count(uti =&gt; uti.acm_iswerktijd.GetValueOrDefault());
                  int aantalAfwezigheidsdagen = unionTijdindelingen.Count(uti =&gt; !uti.acm_iswerktijd.GetValueOrDefault(false) &amp;&amp; !uti.acm_redenvanafwezigheid.Id.Equals(feestDagGuid));
              }
              catch (FaultException&lt;OrganizationServiceFault&gt; e)
              {
                  tracingService.Trace("Exception: {0}", e.ToString());
  
                  // Handle the exception.
                  throw;
              }
  
              tracingService.Trace("Exiting GenerateTijdIndelingen.Execute(), Correlation Id: {0}", context.CorrelationId);
          }
          private static Guid GetAfwezigheidsRedenId(XrmServiceContext serviceContext, string type)
          {
              return (from afwr in serviceContext.acm_afwezigheidsredenSet
                      where afwr.acm_name.ToLower(CultureInfo.InvariantCulture).Equals(type.ToLower(CultureInfo.InvariantCulture))
                      select afwr.Id).First();
          }
  
          private static IEnumerable&lt;acm_afwezigheden&gt; GetAfwezighedenForPersoneelslid(XrmServiceContext serviceContext, Guid personeelslidGuid)
          {
              return (from afw in serviceContext.acm_afwezighedenSet
                      where afw.acm_personeelslid.Id.Equals(personeelslidGuid)
                      select afw);
          }
  
          private static IEnumerable&lt;acm_weekkalender&gt; GetWeekKalender(XrmServiceContext serviceContext, Guid personeelslidGuid)
          {
              return (from wk in serviceContext.acm_weekkalenderSet
                      where wk.acm_werknemer.Id.Equals(personeelslidGuid)
                      select wk);
          }
  
          private static bool IsInNMonthsOrLess(DateTime checkedDateTime, int n)
          {
              DateTime nowDateTime = DateTime.Now;
              DateTime nMonthsDateTime = nowDateTime.AddMonths(n);
              return (nowDateTime &lt;= checkedDateTime &amp;&amp; checkedDateTime &lt;= nMonthsDateTime);
          }
  
          private static IEnumerable&lt;CalendarRule&gt; GetCalendarRules(XrmServiceContext serviceContext)
          {
  
  
              Organization org = (from o in serviceContext.OrganizationSet
                                  select o).FirstOrDefault();
  
              if (org != null &amp;&amp; org.BusinessClosureCalendarId != null)
              {
                  Guid businessClosureCalendarId = org.BusinessClosureCalendarId.Value;
                  Calendar businessClosureCalendar = (from c in serviceContext.CalendarSet
                                                      where c.CalendarId == businessClosureCalendarId
                                                      select c).FirstOrDefault();
  
                  if (businessClosureCalendar != null)
                  {
                      return businessClosureCalendar.CalendarRules;
                  }
              } return null;
          }
          private enum DagDeel
          {
              Vm,
              Nm
          }
          private static string GetOptionsetText(string optionsetName, int optionsetValue, IOrganizationService service)
          {
              string optionsetSelectedText = string.Empty;
              RetrieveOptionSetRequest retrieveOptionSetRequest =
                       new RetrieveOptionSetRequest
                       {
                           Name = optionsetName
                       };
  
              // Execute the request.
              RetrieveOptionSetResponse retrieveOptionSetResponse =
                  (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);
  
              // Access the retrieved OptionSetMetadata.
              OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;
  
              // Get the current options list for the retrieved attribute.
              OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();
              foreach (OptionMetadata optionMetadata in optionList)
              {
                  if (optionMetadata.Value == optionsetValue)
                  {
                      optionsetSelectedText = optionMetadata.Label.UserLocalizedLabel.Label;
                      break;
                  }
              }
              return optionsetSelectedText;
          }
  
          private class TijdIndelingComparer : IEqualityComparer&lt;acm_tijdindeling&gt;
          {
              public bool Equals(acm_tijdindeling ati1, acm_tijdindeling ati2)
              {
                  return ati1.acm_starttijdstip.GetValueOrDefault().Date.Equals(ati2.acm_starttijdstip.GetValueOrDefault().Date);
              }
  
              public int GetHashCode(acm_tijdindeling ati)
              {
                  return ati.acm_starttijdstip.GetHashCode();
              }
          }
      }
  }
  </code></pre>
  
  <p>the 3 lines at the end of the execute are just for eventual debugging, once I get it to work.</p>
  
  <p>I've tried to google why it fails, but I can't seem to find any info on this.</p>
  
  <p>edit: clarified what exactly causes the workflow errors.</p>