コード例 #1
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);
            }
        }
コード例 #2
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);
            }
        }