Exemple #1
0
        protected virtual async Task ProcessAsync(MessageHandleContext handleContext)
        {
            if (handleContext.Message is string)
            {
                await PostProcessAsync(handleContext);

                return;
            }

            var messageType  = handleContext.Message.GetType();
            var handlerTypes = Provider.LookupHandlerTypes(messageType);

            using (ObjectProvider.CreateScope())
            {
                var results = new MessageHandleResult[handlerTypes.Length];
                var index   = 0;
                foreach (var handlerType in handlerTypes)
                {
                    using (ObjectProvider.CreateScope())
                    {
                        Exception error    = null;
                        var       method   = handlerType.GetMethod("HandleAsync", new[] { messageType });
                        var       instance = ObjectProvider.GetService(handlerType);
                        try
                        {
                            var result = (Task)method?.Invoke(instance, new[] { handleContext.Message });
                            if (result != null)
                            {
                                await result;
                            }
                        }
                        catch (Exception ex)
                        {
                            error = ex;
                        }

                        var handleResult = new MessageHandleResult(handlerType, error);
                        results[index] = handleResult;
                        index++;
                    }
                }

                handleContext.SetHandleResult(results);
            }

            await PostProcessAsync(handleContext);
        }