Exemplo n.º 1
0
        public static bool TryToRoute(PublishedMessage sender, Subscription receiver, out MessageRoute route, out PublisherSubscriberMismatch mismatch)
        {
            route    = null;
            mismatch = null;

            var transportsMatch = (sender.Transports ?? new string[0]).Contains(receiver.Destination.Scheme);

            var contentType = SelectContentType(sender, receiver);

            if (transportsMatch && contentType.IsNotEmpty())
            {
                route = new MessageRoute(sender.DotNetType, receiver.Destination, contentType)
                {
                    Publisher = sender.ServiceName,
                    Receiver  = receiver.ServiceName
                };
                return(true);
            }

            mismatch = new PublisherSubscriberMismatch(sender, receiver)
            {
                IncompatibleTransports   = !transportsMatch,
                IncompatibleContentTypes = contentType == null
            };

            return(false);
        }
Exemplo n.º 2
0
        private async Task <List <MessageRoute> > compileRoutes(Type messageType)
        {
            var list = new List <MessageRoute>();

            var modelWriter = _serializers.WriterFor(messageType);
            var supported   = modelWriter.ContentTypes;

            foreach (var channel in _channels.AllKnownChannels().Where(x => x.ShouldSendMessage(messageType)))
            {
                var contentType = supported.FirstOrDefault(x => x != "application/json") ?? "application/json";

                if (contentType.IsNotEmpty())
                {
                    list.Add(new MessageRoute(messageType, modelWriter, channel, contentType)
                    {
                    });
                }
            }

            var subscriptions = await _subscriptions.GetSubscribersFor(messageType);

            if (subscriptions.Any())
            {
                var published = new PublishedMessage(messageType, modelWriter, _channels);


                foreach (var subscription in subscriptions)
                {
                    if (MessageRoute.TryToRoute(published, subscription, out MessageRoute route,
                                                out PublisherSubscriberMismatch mismatch))
                    {
                        route.Writer  = modelWriter[route.ContentType];
                        route.Channel = _channels.GetOrBuildChannel(route.Destination);
                        list.Add(route);
                    }
                    else
                    {
                        _logger.SubscriptionMismatch(mismatch);
                    }
                }