Beispiel #1
0
        private async ValueTask ProcessRichMessageHandlingExceptionAsync(IMessageScope scope, IMessageHandlerFactory asyncHandlerFactory, Type messageType, IMessage message, IRichMessageDescriptor descriptor, List <Exception> exceptions)
        {
            var handlerInstance = asyncHandlerFactory.GetHandler(scope);

            try
            {
                if (handlerInstance == null)
                {
                    throw new ArgumentNullException($"Registered async rich message handler for message type {messageType.Name} is null!");
                }

                var ifType = typeof(IAsyncRichMessageHandler <>).MakeGenericType(messageType);

                var method = ifType.GetMethod(
                    nameof(IAsyncRichMessageHandler <IMessage> .HandleMessageAsync),
                    new[] { messageType, typeof(IRichMessageDescriptor) }
                    );



                await(ValueTask) method.Invoke(handlerInstance, new object[] { message, descriptor });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                asyncHandlerFactory.ReleaseHandler(scope, handlerInstance);
            }
        }
Beispiel #2
0
        private async ValueTask ProcessMessageHandlingExceptionAsync(IMessageScope scope, IMessageHandlerFactory asyncHandlerFactory, Type messageType, IMessage message, List <Exception> exceptions)
        {
            var asyncEventHandler = asyncHandlerFactory.GetHandler(scope);

            try
            {
                if (asyncEventHandler == null)
                {
                    throw new ArgumentNullException($"Registered async message handler for message type {messageType.Name} is null!");
                }

                var asyncHandlerType = typeof(IAsyncMessageHandler <>).MakeGenericType(messageType);

                var method = asyncHandlerType.GetMethod(
                    nameof(IAsyncMessageHandler <IMessage> .HandleMessageAsync),
                    new[] { messageType }
                    );

                await(ValueTask) method.Invoke(asyncEventHandler, new object[] { message });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                asyncHandlerFactory.ReleaseHandler(scope, asyncEventHandler);
            }
        }
Beispiel #3
0
        private void ProcessMessageHandlingException(IMessageScope scope, IMessageHandlerFactory handlerFactory, Type messageType, IMessage message, List <Exception> exceptions)
        {
            var eventHandler = handlerFactory.GetHandler(scope);

            try
            {
                if (eventHandler == null)
                {
                    throw new ArgumentNullException($"Registered message handler for message type {messageType.Name} is null!");
                }

                var handlerType = typeof(IMessageHandler <>).MakeGenericType(messageType);

                var method = handlerType.GetMethod(
                    nameof(IMessageHandler <IMessage> .HandleMessage),
                    new[] { messageType }
                    );

                method.Invoke(eventHandler, new object[] { message });
            }
            catch (TargetInvocationException ex)
            {
                exceptions.Add(ex.InnerException);
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
            finally
            {
                handlerFactory.ReleaseHandler(scope, eventHandler);
            }
        }
Beispiel #4
0
        public async ValueTask <bool> CallAsync(IMessageScope scope, IMessageHandlerFactory handlerFactory, IMessage message, IRichMessageDescriptor messageDescriptor, List <Exception> exceptions = null, CancellationToken cancellationToken = default)
        {
            var             handlerDescriptor = handlerFactory.GetHandlerDescriptor();
            IMessageHandler handler           = null;

            try
            {
                handler = handlerFactory.GetHandler(scope);

                if (handlerDescriptor.IsAsync)
                {
                    await GetOrCreateAsyncHandlerCallCache(handlerDescriptor).Invoke(handler, message, messageDescriptor);
                }
                else
                {
                    GetOrCreateSyncHandlerCallCache(handlerDescriptor).Invoke(handler, message, messageDescriptor);
                }
            }
            catch (TargetInvocationException e)
            {
                exceptions?.Add(e.InnerException);
                return(false);
            }
            catch (Exception e)
            {
                exceptions?.Add(e);
                return(false);
            }
            finally
            {
                if (handler != null)
                {
                    handlerFactory.ReleaseHandler(scope, handler);
                }
            }
            return(true);
        }