C# Dynamics CRM 2011 InvalidWorkflowException

C# Dynamics CRM 2011 InvalidWorkflowException,c#,dynamics-crm-2011,workflow-foundation-4,C#,Dynamics Crm 2011,Workflow Foundation 4,我有两个自定义实体和一个日历规则列表,我试图将每个日历规则解析为第三个自定义实体的单独列表,每天1或2个。然后,我将这些列表合并在一起,形成包含正确实体的最终列表 在生成所有实体完成工作流之前,我正在尝试调试工作流,但我必须首先修复几个处理错误 我得到的错误: Unhandled Exception: System.Activities.InvalidWorkflowException: The following errors were encountered while processing

我有两个自定义实体和一个日历规则列表,我试图将每个日历规则解析为第三个自定义实体的单独列表,每天1或2个。然后,我将这些列表合并在一起,形成包含正确实体的最终列表

在生成所有实体完成工作流之前,我正在尝试调试工作流,但我必须首先修复几个处理错误

我得到的错误:

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<T> or LambdaReference<T>.
'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)
给出以下错误的代码:

// <copyright file="GenerateTijdIndelingen.cs" company="">
// Copyright (c) 2013 All Rights Reserved
// </copyright>
// <author></author>
// <date>11/22/2013 12:28:26 PM</date>
// <summary>Implements the GenerateTijdIndelingen Workflow Activity.</summary>

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<EntityReference> Personeelsfichereference { get; set; }
        /// <summary>
        /// Executes the workflow activity.
        /// </summary>
        /// <param name="executionContext">The execution context.</param>
        protected override void Execute(CodeActivityContext executionContext)
        {


            // Create the tracing service
            ITracingService tracingService = executionContext.GetExtension<ITracingService>();

            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<IWorkflowContext>();

            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<IOrganizationServiceFactory>();
            // 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<slfn_personeelsfiche>(executionContext).Id;
                IEnumerable<CalendarRule> applicableCalendarRules = GetCalendarRules(serviceContext).Where(cr => IsInNMonthsOrLess(cr.StartTime.GetValueOrDefault(), 2));
                Guid feestDagGuid = GetAfwezigheidsRedenId(serviceContext, "Feestdag");
                IEnumerable<acm_weekkalender> weekkalenders = GetWeekKalender(serviceContext, personeelslidGuid);
                IEnumerable<acm_afwezigheden> applicableAfwezigheden = GetAfwezighedenForPersoneelslid(serviceContext, personeelslidGuid).Where(afw => IsInNMonthsOrLess(afw.acm_eerstedag.GetValueOrDefault(), 2) || IsInNMonthsOrLess(afw.acm_laatstedag.GetValueOrDefault(), 2));
                List<acm_tijdindeling> feestdagTijdindelingen = new List<acm_tijdindeling>();

                //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<acm_tijdindeling> afwezigheidTijdIndelingen = new List<acm_tijdindeling>();
                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<acm_tijdindeling> werktijdIndelingen = new List<acm_tijdindeling>();
                foreach (acm_weekkalender acmWeekkalender in weekkalenders)
                {
                    DateTime contractstartDateTime = DateTime.Now;
                    DateTime contractendDateTime = contractstartDateTime.AddMonths(2);
                    List<DayOfWeek> dayOfWeeks = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>().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<Boolean>("acm_" + weekdag + lowerDagDeel + "vrijaf")) continue;
                            int startuur = acmWeekkalender.GetAttributeValue<OptionSetValue>("acm_" + weekdag + lowerDagDeel + "startuur").Value;
                            int startminuut = acmWeekkalender.GetAttributeValue<OptionSetValue>("acm_" + weekdag + lowerDagDeel + "startminuut").Value;
                            int einduur = acmWeekkalender.GetAttributeValue<OptionSetValue>("acm_" + weekdag + lowerDagDeel + "einduur").Value;
                            int eindminuut = acmWeekkalender.GetAttributeValue<OptionSetValue>("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 < 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<acm_tijdindeling> unionTijdindelingen = feestdagTijdindelingen.Union(afwezigheidTijdIndelingen, new TijdIndelingComparer()).Union(werktijdIndelingen, new TijdIndelingComparer()).ToList();
                int aantalFeestdagen = unionTijdindelingen.Count(uti => uti.acm_redenvanafwezigheid.Id.Equals(feestDagGuid));
                int aantalwerkdagen = unionTijdindelingen.Count(uti => uti.acm_iswerktijd.GetValueOrDefault());
                int aantalAfwezigheidsdagen = unionTijdindelingen.Count(uti => !uti.acm_iswerktijd.GetValueOrDefault(false) && !uti.acm_redenvanafwezigheid.Id.Equals(feestDagGuid));
            }
            catch (FaultException<OrganizationServiceFault> 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<acm_afwezigheden> GetAfwezighedenForPersoneelslid(XrmServiceContext serviceContext, Guid personeelslidGuid)
        {
            return (from afw in serviceContext.acm_afwezighedenSet
                    where afw.acm_personeelslid.Id.Equals(personeelslidGuid)
                    select afw);
        }

        private static IEnumerable<acm_weekkalender> 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 <= checkedDateTime && checkedDateTime <= nMonthsDateTime);
        }

        private static IEnumerable<CalendarRule> GetCalendarRules(XrmServiceContext serviceContext)
        {


            Organization org = (from o in serviceContext.OrganizationSet
                                select o).FirstOrDefault();

            if (org != null && 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<acm_tijdindeling>
        {
            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();
            }
        }
    }
}
//
//版权所有(c)2013保留所有权利
// 
// 
//2013年11月22日下午12:28:26
//实现GenerateJDindelingen工作流活动。
使用System.Collections.Generic;
利用制度全球化;
使用System.Linq;
使用Microsoft.Xrm.Sdk.Messages;
使用Microsoft.Xrm.Sdk.Metadata;
命名空间acm.mscrm2011.hrpro.Workflow
{
使用制度;
使用系统活动;
使用System.ServiceModel;
使用Microsoft.Xrm.Sdk;
使用Microsoft.Xrm.Sdk.Workflow;
公共密封类GenerateJDIndelingen:CodeActivity
{
[输入(“个人信息”)]
[参考目标(slfn_personeelsfiche.EntityLogicalName)]
公共非授权人员引用{get;set;}
/// 
///执行工作流活动。
/// 
///执行上下文。
受保护的覆盖无效执行(CodeActivityContext executionContext)
{
//创建跟踪服务
ITracingService tracingService=executionContext.GetExtension();
如果(tracingService==null)
{
抛出新的InvalidPlugineExecutionException(“检索跟踪服务失败”);
}
tracingService.Trace(“输入generateJDindelingen.Execute(),活动实例Id:{0},工作流实例Id:{1}”,
executionContext.ActivityInstanceId,
executionContext.WorkflowInstanceId);
//创建上下文
IWorkflowContext=executionContext.GetExtension();
if(上下文==null)
{
抛出新的InvalidPluginExecutionException(“检索工作流上下文失败”);
}
tracingService.Trace(“GenerateTijIndelingen.Execute(),相关Id:{0},启动用户:{1}”,
context.CorrelationId,
上下文。InitiatingUserId);
IOOrganizationServiceFactory服务工厂=executionContext.GetExtension();
//这就是魔法发生的地方
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);
IOOrganizationService=serviceFactory.CreateOrganizationService(context.UserId);
XrmServiceContext serviceContext=新的XrmServiceContext(服务);
尝试
{
//大口蛤蚧
Guid personeelslidGuid=PersoneElsFicherReference.Get(executionContext).Id;
IEnumerable ApplicatableCalendarRules=GetCalendarRules(serviceContext)。其中(cr=>IsInMonthSorless(cr.StartTime.GetValueOrDefault(),2));
Guid feestDagGuid=getafwezigheidsredinid(serviceContext,“Feestdag”);
IEnumerable WeekCalenders=GetWeekCalender(serviceContext,PersonelSlidGuid);
IEnumerable applicatableAfwezigheden=GetAfwezighedenForPersoneelslid(serviceContext,PersonelElsLidGuid)。其中(afw=>IsInMonthOrless(afw.acm_eerstedag.GetValueOrDefault(),2)| IsInMonthOrless(afw.acm_laatstedag.GetValueOrDefault(),2));
List feestdagTijdindelingen=新列表();
//日历规则解析
foreach(适用日历规则中的日历规则)
{
acm\u tijdindeling feestdagtijdindeling=new acm\u tijdindeling{acm\u personeelsfiche=new EntityReference(slfn\u personeelsfiche.EntityLogicalName,personeelslidGuid),acm\u starttijdstip=calendarRule.StartTime,acm\u eindtijdstip=calendarRule.EndTime.GetValueOrDefault(),acm\u iswerktijd=false,acm\u RedenvanaRefezigheid=new EntityReference(acm_afwezigheidsreden.EntityLogicalName,feestDagGuid)};
feestdagtijdindleing.acm_name=String.Format(feestdagtijdindleing.acm_personelsfiche.name+“-”+feestdagtijdindleing.acm_redenvanawezigheid.name+“-”+feestdagtijdindleing.acm_starttijdstip.GetValueOrDefault().toSortDateString());
添加(feestdagtijdindleing);
}
//阿夫韦齐赫登·帕森
List AFWEZIGHIDETIJDINDELINGEN=新列表();
foreach(acm_afwezigheden Acmafwezigheden适用于Ezigheden)
{
acm_tijdineling afwezigheidtijdineling=new acm_tijdineling{acm_personelsfiche=new EntityReference(slfn_personelsfiche.entityname,acmAfwezigheid.acm_personelslid.Id),acm_starttijdstip=acmAfwezigheid.acm_eerstedag.GetValueOrDefault(),acm_eindtijdptip=acmAfwezigheid.acm.laatstedag.GetValueOrDefault(),acm_iswerktijd=false,acm_redenvanawezigheid=new EntityReference(acm_afwezigheidsreden.EntityLogicalName,acmAfwezigheid.acm_afwezigheidsreden.Id)};
afwezigheidtijdindleing.acm_name=String.Format(afwezigheidtijdindleing.acm_personelsfiche.name+“-”+afwezigheidtijdindleing.acm_redenvanawezigheid.name+“-”+afwezigheidtijdindleing.acm_starttijdistip.GetValueOrDefault().ToShortDateString());
添加(afwezigheidtijdineling);
}
//帕森周刊
List-werktijdIndelingen=新列表();
foreach(acm_weekkalender acm weekkalender in weekkalender)
{
日期时间合同开始日期时间