Exemplo n.º 1
0
        private void AddQuery(Type queryType, Type handlerType)
        {
            var methodInfo = handlerType.GetMethod(nameof(IQueryHandler <IQuery <object>, object> .HandleAsync), new Type[] { queryType, typeof(CancellationToken) });

            if (methodInfo == null)
            {
                throw new InvalidOperationException($"在消息处理程序({handlerType.Name})中,未找到消息({queryType.Name})的处理方法");
            }

            var router = _queryRoutes.FirstOrDefault(x => x.MessageType == queryType);

            if (router == null)
            {
                router = new MessageHandlerRouter
                {
                    MessageType = queryType,
                    HandlerType = handlerType,
                    MethodInfo  = methodInfo,
                };
                _queryRoutes.Add(router);
            }
            else
            {
                router.HandlerType = handlerType;
                router.MethodInfo  = methodInfo;
            }
        }
Exemplo n.º 2
0
        private void AddCommand(Type commandType, Type handlerType)
        {
            var methodInfo = handlerType.GetMethod(nameof(ICommandHandler <ICommand> .HandleAsync), new Type[] { commandType, typeof(CancellationToken) });

            if (methodInfo == null)
            {
                throw new InvalidOperationException($"在消息处理程序({handlerType.Name})中,未找到消息({commandType.Name})的处理方法");
            }

            var router = _commandRoutes.FirstOrDefault(x => x.MessageType == commandType);

            if (router == null)
            {
                router = new MessageHandlerRouter
                {
                    MessageType = commandType,
                    HandlerType = handlerType,
                    MethodInfo  = methodInfo,
                };
                _commandRoutes.Add(router);
            }
            else
            {
                router.HandlerType = handlerType;
                router.MethodInfo  = methodInfo;
            }
        }