[英]Dynamics CRM 2011 InvalidWorkflowException
我有2个自定义实体和一个日历规则列表,我试图将每个规则解析为第3个自定义实体的单独列表,每天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();
}
}
}
}
一旦我开始执行,执行末尾的3行仅用于最终调试。
我试图用谷歌搜索为什么失败了,但是我似乎找不到任何信息。
编辑:弄清究竟是什么导致工作流错误。
现在这是一个相当古老的问题,但是我仍然可以回答,因此有记录可供其他人查找。
问题实际上在于工作流程过程创建的执行方式。 要分析自定义工作流程,需要一个引用该工作流程的步骤。 该参考将输入到流程创建窗口底部的面板中。 添加步骤后,将出现一个文本框,邀请您添加描述。 在任何情况下都不要输入任何东西。 尝试手动运行工作流程时,此文本框中的任何内容都会导致失败。
比较我的描述:“ Exercises CreateQuoteApprovalMatrix”-错误消息:“ CustomActivityStep1:练习CreateQuoteApprovalMatrix_1_converted”。 删除说明后,我就可以成功调试。
我发现这个问题与代码本身无关。 我收到了90%的代码被注释掉的错误。 显然,工作流调试器如何处理早期绑定是一个问题。 一旦我注释掉早期绑定,它就会产生另一个与插件调试器本身有关的错误,该错误甚至只用3行代码即服务注册就触发了。
因此,我决定使用其他调试方法,即明智地使用traceInfos。 我还从中注意到,我应该首先在本地测试我的应用程序,因为使用DateTimes处理循环时,无限循环的风险很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.