Custom workflow step inconsistent behavior

时间:2016-07-11 19:36:25

标签: plugins dynamics-crm-2013

I've made a custom workflow step which sends email to the group of users. I take precreated email and send it programmatically:

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);
    }

The problem is - it works just fine in sync mode, and throw an "Invalid Cast" exception if i convert workflow into async and try to run it.

Here what trace log on the server provides:

>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)

Somehow error occurs during converting results of the workflow. Where should I look to find the root of this problem?

        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

1 个答案:

答案 0 :(得分:2)

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

遇到了与核心电子邮件操作类似的问题
  

我已将服务实例化更改为正在运行的用户,所有工作都没有任何问题:

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