自定义工作流步骤不一致行为

Custom workflow step inconsistent behavior

我创建了一个自定义工作流程步骤,用于向用户组发送电子邮件。我使用预先创建的电子邮件并以编程方式发送它:

if (recepients.Entities.Any())
    {
        Entity email = service.Retrieve("email", mailId, new ColumnSet(true));

        email.Attributes["to"] = recepients.Entities.ToArray();

        service.Update(email);

        SendEmailRequest sendEmail = new SendEmailRequest()
        {
           EmailId = mailId,
           IssueSend = true
        };

        service.Execute(sendEmail);
    }

问题是 - 它在同步模式下工作得很好,如果我将工作流转换为异步并尝试 运行 它会抛出 "Invalid Cast" 异常。

这里提供了服务器上的跟踪日志:

>System.InvalidCastException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #EBCA9EC1: System.InvalidCastException: Specified cast is not valid.

>   at Microsoft.Crm.Common.ObjectModel.TrackingManager.GetNextTrackingToken(String subject, String& trackingToken)

>   at Microsoft.Crm.Common.ObjectModel.EmailService.Send(Guid emailId, Boolean issueSend, String trackingToken, ExecutionContext context)

转换工作流结果时出现某种错误。我应该去哪里寻找这个问题的根源?

        at WorkflowToAsyncResultConverter.Convert(WorkflowSystemPausedResult wfResult)  ilOffset = 0x23
        at WorkflowToAsyncResultConverter.Convert()  ilOffset = 0x9D

        at WorkflowContext.EndProcessing(IGenericHandlerResult result)  ilOffset = 0xC

        at ActivityHost.CompleteWorkflow(IGenericHandlerResult result, WorkflowApplication activityInstance, ICommonWorkflowContext context)  ilOffset = 0x70

        at ActivityHostBase.OnWorkflowTerminated(WorkflowApplicationUnhandledExceptionEventArgs args, WorkflowApplication activityInstance, ICommonWorkflowContext context)  ilOffset = 0x8F

        at UnhandledExceptionEventHandler.OnStage1Complete(IAsyncResult lastResult, WorkflowApplication instance, Exception exception, Activity source, String sourceInstanceId)  ilOffset = 0x46

        at UnhandledExceptionEventHandler.Run(WorkflowApplication instance, Exception exception, Activity exceptionSource, String exceptionSourceInstanceId)  ilOffset = 0x78

        at WorkflowApplication.OnNotifyUnhandledException(Exception exception, Activity exceptionSource, String exceptionSourceInstanceId)  ilOffset = 0x23

        at ActivityExecutor.NotifyUnhandledException(Exception exception, ActivityInstance source)  ilOffset = 0x18

        at Scheduler.OnScheduledWork(Object state)  ilOffset = 0x123

        at SendOrPostThunk.UnhandledExceptionFrame(Object state)  ilOffset = 0x0

        at ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)  ilOffset = 0x22

        at IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)  ilOffset = 0x5

        at _IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)  ilOffset = 0x3C

尝试创建一个 OrganizationService 运行 作为执行用户,而不是 SYSTEM。 Andrii Butenko, who concluded that it works when running the service in the context of the executing user.

也遇到过核心电子邮件操作的类似问题

I have changed instantiation of service to running user and everything worked without any issues:

 IOrganizationServiceFactory factory = 
     (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
 IOrganizationService service = factory.CreateOrganizationService(executioncontext.UserId);