public async Task Process(IncomingMessage message, Context context)
        {
            var messageTypeName = message.MessageTypeNames.First();

            if (!handlerBindings.ContainsKey(messageTypeName))
            {
                throw new InvalidOperationException(
                          $"Subscriber was handed a message with type-name {messageTypeName} but does not contain a handler for that type.");
            }

            // TODO Unit tests
            if (handlerBindings[messageTypeName].Count == 1)
            {
                await handlerBindings[messageTypeName][0]
                .Handle(message, context)
                .ConfigureAwait(false);
                return;
            }

            var specificHandler = message.Headers.GetValueOrDefault("SpecificHandler");

            if (specificHandler != null)
            {
                var handlerBinding = handlerBindings[messageTypeName]
                                     .Single(binding => binding.HandlerType.FullName == specificHandler);

                await handlerBinding
                .Handle(message, context)
                .ConfigureAwait(false);

                return;
            }

            // TODO Allocations review
            var messages = new List <OutgoingMessage>();

            foreach (var handlerBinding in handlerBindings[messageTypeName])
            {
                var outgoingHeaders = new[]
                {
                    new Header("SpecificHandler", handlerBinding.HandlerType.FullName)
                };

                messages.Add(
                    new OutgoingMessage(
                        Guid.NewGuid().ToString(),
                        message.Body,
                        message.MessageTypeNames,
                        outgoingHeaders,
                        endpointName));
            }

            await outgoingPipeline
            .Process(messages)
            .ConfigureAwait(false);
        }
Esempio n. 2
0
 public Task PublishEvent(object eventToPublish, IEnumerable <Header> additionalHeaders = null)
 => outgoingPipeline.Process(
     new[] {
     MapEventToMessage(eventToPublish, additionalHeaders)
 });