Esempio n. 1
0
 internal MessageFilteringContext(
     IEnumerator <IMessageFilter> filtersEnumerator,
     object state,
     object message,
     MessageHeaders headers,
     MessageHandlingContext handlingContext,
     Func <Task <MessageHandlingResult> > nativeHandler)
 {
     State              = state;
     Message            = message;
     Headers            = headers;
     HandlingContext    = handlingContext;
     _nativeHandler     = nativeHandler;
     _filtersEnumerator = filtersEnumerator;
 }
        public Task <MessageHandlingResult> InvokeHandlerAsync(
            IServiceProvider parentServiceProvider,
            object message,
            MessageHeaders headers,
            MessageHandlingContext handlingContext,
            IMessagePublisher publisher)
        {
            if (parentServiceProvider == null)
            {
                throw new ArgumentNullException(nameof(parentServiceProvider));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            if (publisher == null)
            {
                throw new ArgumentNullException(nameof(publisher));
            }

            if (message is TMessage typedMessage)
            {
                using (var scope = parentServiceProvider.CreateScope())
                {
                    var handler = (IMessageHandler <TMessage>)scope.ServiceProvider.GetRequiredService(_options.HandlerType);
                    var context = new MessageFilteringContext
                                  (
                        _globalFilters.GetEnumerator(),
                        null,
                        typedMessage,
                        headers,
                        handlingContext,
                        () => handler.HandleAsync(typedMessage, headers, publisher)
                                  );

                    return(context.InvokeNextAsync());
                }
            }

            throw new ArgumentException($"Object of type {typeof(TMessage)} is expected, but {message.GetType()} is passed", nameof(message));
        }