コード例 #1
0
        public async Task <TOut> SendCommand <TOut>(IChakadRequest <TOut> command, TimeSpan?timeout = null,
                                                    Action <Exception, TimeSpan> action             = null, SendOptions options = null) where TOut : ChakadResult, new ()
        {
            var send = await pipeline.StartProcess(command, timeout, action, options);

            return(send);
        }
コード例 #2
0
        private async Task <TOut> InvokeMessageHandle <TOut>(IChakadRequest <TOut> command, Type eventHandler, object instance)
            where TOut : ChakadResult, new()
        {
            var res = (from info in eventHandler.GetMethods()
                       where info.Name.ToLower() == "handle"
                       select info.Invoke(instance, new object[] { command }))
                      .FirstOrDefault();

            var task1 = res as Task <TOut>;

            if (task1 == null)
            {
                return(null);
            }

            if (task1.Exception != null)
            {
                return new TOut
                       {
                           Succeeded            = false,
                           AggregatedExceptions = task1.Exception,
                           Message = task1.Exception.GetaAllMessages()
                       }
            }
            ;

            var task = await task1;

            return(task);
        }
コード例 #3
0
        public async Task <TOut> Send <TOut>(IChakadRequest <TOut> command, TimeSpan?timeout = null,
                                             Action <Exception, TimeSpan> action             = null, SendOptions options = null) where TOut : ChakadResult, new()
        {
            var commandType = command.GetType();

            var baseType = command.GetType().BaseType;

            if (baseType == null)
            {
                throw new Exception();
            }

            var eventHandler = ChakadContainer.ResolveCommandHandler(commandType);

            if (eventHandler == null)
            {
                throw new ChakadPipelineNotFoundHandler(@"Not found handler for {0}", command);
            }

            if (timeout == null)
            {
                timeout = new TimeSpan(0, 0, 0, 0, 500);
            }

            if (action == null)
            {
                action = (ex, time) =>
                {
                    //TODO log ex
                    Console.WriteLine(ex.ToString());
                };
            }

            var policy = Policy.Handle <Exception>()
                         .WaitAndRetryAsync(1, retryAttempt => timeout.Value, action);

            using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
            {
                var handler = scope.ResolveOptional(eventHandler);

                TOut result = null;

                await policy.ExecuteAsync(async() =>
                {
                    result = await InvokeMessageHandle(command, eventHandler, handler);
                });

                return(result);
            }
        }
コード例 #4
0
        private async Task <TOut> InvokeMessageHandle <TOut>(IChakadRequest <TOut> command,
                                                             Type eventHandler,
                                                             object instance)
            where TOut : ChakadResult, new()
        {
            Logger.LogInformation(EventIdConstants.CommandStartInvokinMessageHandle,
                                  command.CorrelationId,
                                  $"InvokeMessageHandle. Correlation_Id is ={command.CorrelationId} Start Invoking Message Handler.");

            var res = (from info in eventHandler.GetMethods()
                       where info.Name.ToLower() == "handle"
                       select info.Invoke(instance, new object[] { command }))
                      .FirstOrDefault();

            var task1 = res as Task <TOut>;

            if (task1 == null)
            {
                return(null);
            }

            if (task1.Exception != null)
            {
                Logger.LogError(EventIdConstants.CommandInvokingMessageHandleWasFaield, task1.Exception,
                                command.CorrelationId,
                                $"InvokeMessageHandle. Correlation_Id is ={command.CorrelationId} Error in Invoking Message Handle.");

                return(new TOut
                {
                    Succeeded = false,
                    AggregatedExceptions = task1.Exception,
                    Message = task1.Exception.GetaAllMessages()
                });
            }
            var task = await task1;

            Logger.LogInformation(EventIdConstants.CommandInvokingMessageHandleWasSuccessfully,
                                  command.CorrelationId,
                                  $"InvokeMessageHandle. Correlation_Id is ={command.CorrelationId} Message Handler invoked successfully.");

            return(task);
        }
コード例 #5
0
 public async Task <TOut> Handle(IChakadRequest <TOut> message)
 {
     return(await InternalHandle((T)message));
 }
コード例 #6
0
        public async Task <TOut> StartProcess <TOut>(IChakadRequest <TOut> command, TimeSpan?timeout = null,
                                                     Action <Exception, TimeSpan> action             = null, SendOptions options = null)
            where TOut : ChakadResult, new()
        {
            var commandType = command.GetType();

            Logger.LogInformation(EventIdConstants.CommandStartProcess, commandType.FullName, command, "Start Process");


            var baseType = command.GetType().BaseType;

            if (baseType == null)
            {
                Logger.LogError(EventIdConstants.CommandBaseTypeIsEmpty, command.CorrelationId, $"StartProcess. Correlation_Id is ={command.CorrelationId} Command base type is null.");

                throw new Exception(@"Command base type is null");
            }

            var eventHandler = ChakadContainer.ResolveCommandHandler(commandType);

            if (eventHandler == null)
            {
                var exeption = new ChakadPipelineNotFoundHandler(@"Not found handler for {0}", command.CorrelationId);

                Logger.LogError(EventIdConstants.CommandNotFounddHandler, exeption, command.CorrelationId, $"StartProcess. Correlation_Id is ={command.CorrelationId}. Not found handler");
                throw exeption;
            }

            if (timeout == null)
            {
                timeout = new TimeSpan(0, 0, 0, 0, 500);
            }

            if (action == null)
            {
                action = (ex, time) =>
                {
                    //TODO log ex
                    Console.WriteLine(ex.ToString());
                };
            }

            var policy = Policy.Handle <Exception>()
                         .WaitAndRetryAsync(1, retryAttempt => timeout.Value, action);

            Logger.LogInformation(EventIdConstants.CommandInitializeCircuteBreaker, command.CorrelationId, $"Start Process. Correlation_Id is ={command.CorrelationId}  Set Retry Count equals to 2 in case of any failures in invoking message handler.");

            using (var scope = ChakadContainer.Autofac.BeginLifetimeScope(ChakadContainer.AutofacScopeName))
            {
                var handler = scope.ResolveOptional(eventHandler);

                TOut result = null;

                await policy.ExecuteAsync(async() =>
                {
                    result = await InvokeMessageHandle(command, eventHandler, handler);
                });

                return(result);
            }
        }