public async Task <object> Connect(TContext context, CancellationToken cancellationToken)
        {
            object result = null;

            try
            {
                await _specification.BeforeExecute(context, cancellationToken).ConfigureAwait(false);

                await _specification.Execute(context, cancellationToken).ConfigureAwait(false);

                if (Next != null)
                {
                    result = await Next.Connect(context, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    result = await ConnectToPipe(context, cancellationToken).ConfigureAwait(false);
                }

                await _specification.AfterExecute(context, cancellationToken).ConfigureAwait(false);

                return(result);
            }
            catch (Exception e)
            {
                var task = _specification.OnException(e, context);
                result = PipeHelper.GetResultFromTask(task);
            }
            return(result);
        }
Example #2
0
        public async Task <object> Connect(TContext context, CancellationToken cancellationToken)
        {
            object result = null;

            try
            {
                await _specification.BeforeExecute(context, cancellationToken);

                await _specification.Execute(context, cancellationToken);

                result = await(Next?.Connect(context, cancellationToken) ?? ConnectToHandler(context, cancellationToken));
                await _specification.AfterExecute(context, cancellationToken);
            }
            catch (Exception e)
            {
                var task = _specification.OnException(e, context);
                result = PipeHelper.GetResultFromTask(task);
            }
            return(result);
        }
Example #3
0
        private async Task <object> ConnectToHandler(TContext context, CancellationToken cancellationToken)
        {
            var handlers = PipeHelper.GetHandlerBindings(context, true, _messageHandlerRegistry);

            if (handlers.Count() > 1)
            {
                throw new MoreThanOneHandlerException(context.Message.GetType());
            }

            var binding = handlers.Single();

            var handlerType = binding.HandlerType;
            var messageType = context.Message.GetType();

            var handleMethod = handlerType.GetRuntimeMethods().Single(m => PipeHelper.IsHandleMethod(m, messageType));

            var handler = (_resolver == null) ? Activator.CreateInstance(handlerType) : _resolver.Resolve(handlerType);

            var task = (Task)handleMethod.Invoke(handler, new object[] { context, cancellationToken });
            await task.ConfigureAwait(false);

            return(PipeHelper.GetResultFromTask(task));
        }