コード例 #1
0
        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);
        }
コード例 #2
0
        private async Task ConnectToHandler(TContext context, CancellationToken cancellationToken)
        {
            var handlerBindings = PipeHelper.GetHandlerBindings(context, false);

            foreach (var handlerBinding in handlerBindings)
            {
                var handlerType = handlerBinding.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);
            }
        }
コード例 #3
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);
        }
コード例 #4
0
        private async Task ConnectToHandler(TContext context, CancellationToken cancellationToken)
        {
            var handlerBindings = PipeHelper.GetHandlerBindings(context, true, _messageHandlerRegistry);

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

            var handlerBinging = handlerBindings.Single();
            var handlerType    = handlerBinging.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);
        }
コード例 #5
0
ファイル: RequestPipe.cs プロジェクト: harry-schurr/mediator
        private async Task <object> ConnectToHandler(TContext context, CancellationToken cancellationToken)
        {
            var handlers = PipeHelper.GetHandlerBindings(context, true);

            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(task.GetType().GetTypeInfo().GetDeclaredProperty("Result").GetValue(task));
        }