Exemplo n.º 1
0
 protected virtual async Task ExecuteTask(ExecutorArgs executorArgs, ServiceMessageEventArgs args)
 {
     try
     {
         var executor = (IExecutor)ActivatorUtilities.CreateInstance(args.Services, executorArgs.TaskInfo.ExecutorType, new object[] { executorArgs });
         executorArgs.TasksManager.CurrentExecutor = executor;
         Task <HandleMessageResult> execResTask = RunExecutor(executor, args);
         if (executor.IsAsyncProcessing && !execResTask.IsCompleted)
         {
             this.ExecuteLongRun(execResTask, executorArgs, args);
         }
         else
         {
             var res = await execResTask;
             args.TaskCompletionSource.TrySetResult(res);
         }
     }
     catch (OperationCanceledException)
     {
         args.TaskCompletionSource.TrySetCanceled(args.Token);
     }
     catch (PPSException ex)
     {
         args.TaskCompletionSource.TrySetException(ex);
     }
     catch (Exception ex)
     {
         Logger.LogError(ErrorHelper.GetFullMessage(ex));
         args.TaskCompletionSource.TrySetException(new PPSException(ex));
     }
 }
Exemplo n.º 2
0
        public override async Task <ServiceMessageEventArgs> HandleMessage(ISSSBService sender, ServiceMessageEventArgs serviceMessageArgs)
        {
            MessageAtributes messageAtributes = null;

            try
            {
                serviceMessageArgs.Token.ThrowIfCancellationRequested();
                XElement xml = serviceMessageArgs.Message.GetMessageXML();
                messageAtributes = xml.GetMessageAttributes();
            }
            catch (OperationCanceledException)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetCanceled(serviceMessageArgs.Token);
                return(serviceMessageArgs);
            }
            catch (PPSException ex)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetException(ex);
                return(serviceMessageArgs);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ErrorHelper.GetFullMessage(ex));
                serviceMessageArgs.TaskCompletionSource.TrySetException(new PPSException(ex));
                return(serviceMessageArgs);
            }

            OnDemandTaskManager taskManager = serviceMessageArgs.Services.GetRequiredService <OnDemandTaskManager>();

            try
            {
                serviceMessageArgs.Token.ThrowIfCancellationRequested();
                var task = await taskManager.GetTaskInfo(messageAtributes.TaskID.Value);

                serviceMessageArgs.TaskID = messageAtributes.TaskID.Value;
                var executorArgs = new ExecutorArgs(taskManager, task, serviceMessageArgs.Message, messageAtributes);
                await ExecuteTask(executorArgs, serviceMessageArgs);
            }
            catch (OperationCanceledException)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetCanceled(serviceMessageArgs.Token);
            }
            catch (PPSException ex)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetException(ex);
            }
            catch (Exception ex)
            {
                Logger.LogCritical(ErrorHelper.GetFullMessage(ex));
                serviceMessageArgs.TaskCompletionSource.TrySetException(new PPSException(ex));
            }

            return(serviceMessageArgs);
        }
        public override async Task <ServiceMessageEventArgs> HandleMessage(ISSSBService sender, ServiceMessageEventArgs serviceMessageArgs)
        {
            MessageAtributes messageAtributes = null;
            SSSBMessage      originalMessage  = null;

            try
            {
                serviceMessageArgs.Token.ThrowIfCancellationRequested();
                XElement envelopeXml         = serviceMessageArgs.Message.GetMessageXML();
                byte[]   originalMessageBody = Convert.FromBase64String(envelopeXml.Element("body").Value);
                XElement originalMessageXml  = originalMessageBody.GetMessageXML();
                messageAtributes               = originalMessageXml.GetMessageAttributes();
                messageAtributes.isDefered     = true;
                messageAtributes.attemptNumber = (int)envelopeXml.Attribute("attemptNumber");
                string messageType    = (string)envelopeXml.Attribute("messageType");
                string serviceName    = (string)envelopeXml.Attribute("serviceName");
                string contractName   = (string)envelopeXml.Attribute("contractName");
                long   sequenceNumber = (long)envelopeXml.Attribute("sequenceNumber");
                MessageValidationType validationType = (MessageValidationType)Enum.Parse(typeof(MessageValidationType), envelopeXml.Attribute("validationType").Value);
                Guid conversationHandle  = Guid.Parse(envelopeXml.Attribute("conversationHandle").Value);
                Guid conversationGroupID = Guid.Parse(envelopeXml.Attribute("conversationGroupID").Value);

                originalMessage = new SSSBMessage(conversationHandle, conversationGroupID, validationType, contractName);
                originalMessage.SequenceNumber = sequenceNumber;
                originalMessage.ServiceName    = serviceName;
                originalMessage.Body           = originalMessageBody;
            }
            catch (OperationCanceledException)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetCanceled(serviceMessageArgs.Token);
                return(serviceMessageArgs);
            }
            catch (PPSException ex)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetException(ex);
                return(serviceMessageArgs);
            }
            catch (Exception ex)
            {
                this.Logger.LogError(ErrorHelper.GetFullMessage(ex));
                serviceMessageArgs.TaskCompletionSource.TrySetException(new PPSException(ex));
                return(serviceMessageArgs);
            }

            OnDemandTaskManager taskManager = serviceMessageArgs.Services.GetRequiredService <OnDemandTaskManager>();

            try
            {
                serviceMessageArgs.Token.ThrowIfCancellationRequested();
                var task = await taskManager.GetTaskInfo(messageAtributes.TaskID.Value);

                serviceMessageArgs.TaskID = messageAtributes.TaskID.Value;
                var executorArgs = new ExecutorArgs(taskManager, task, originalMessage, messageAtributes);
                await ExecuteTask(executorArgs, serviceMessageArgs);
            }
            catch (OperationCanceledException)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetCanceled(serviceMessageArgs.Token);
            }
            catch (PPSException ex)
            {
                serviceMessageArgs.TaskCompletionSource.TrySetException(ex);
            }
            catch (Exception ex)
            {
                Logger.LogCritical(ErrorHelper.GetFullMessage(ex));
                serviceMessageArgs.TaskCompletionSource.TrySetException(new PPSException(ex));
            }

            return(serviceMessageArgs);
        }
Exemplo n.º 4
0
 protected virtual void ExecuteLongRun(Task <HandleMessageResult> execResTask, ExecutorArgs executorArgs, ServiceMessageEventArgs serviceArgs)
 {
     var continuationTask = execResTask.ContinueWith((antecedent) =>
     {
         try
         {
             if (antecedent.IsFaulted)
             {
                 antecedent.Exception.Flatten().Handle((err) =>
                 {
                     serviceArgs.TaskCompletionSource.TrySetException(err);
                     return(true);
                 });
             }
             else if (antecedent.IsCanceled)
             {
                 serviceArgs.TaskCompletionSource.TrySetCanceled(serviceArgs.Token);
             }
             else
             {
                 serviceArgs.TaskCompletionSource.TrySetResult(antecedent.Result);
             }
         }
         catch (OperationCanceledException)
         {
             serviceArgs.TaskCompletionSource.TrySetCanceled(serviceArgs.Token);
         }
         catch (PPSException ex)
         {
             serviceArgs.TaskCompletionSource.TrySetException(ex);
         }
         catch (Exception ex)
         {
             Logger.LogError(ErrorHelper.GetFullMessage(ex));
             serviceArgs.TaskCompletionSource.TrySetException(new PPSException(ex));
         }
     }, TaskContinuationOptions.ExecuteSynchronously);
 }