Exemplo n.º 1
0
    string SerializeEnclosedMessageTypes(Type messageType)
    {
        var metadata = messageMetadataRegistry.GetMessageMetadata(messageType);
        IEnumerable <Type> distinctTypes = metadata.MessageHierarchy.Distinct();

        return(string.Join(";", distinctTypes.Select(t => t.AssemblyQualifiedName)));
    }
 IEnumerable <MessageMetadata> GetMessageMetadata(string messageTypeIdentifier)
 {
     return(messageTypeIdentifier
            .Split(';')
            .Select(type => messageMetadataRegistry.GetMessageMetadata(type))
            .Where(metadata => metadata != null));
 }
    List <LogicalMessage> Extract(TransportMessage physicalMessage)
    {
        if (physicalMessage.Body == null || physicalMessage.Body.Length == 0)
        {
            return(new List <LogicalMessage>());
        }

        string messageTypeIdentifier;
        List <MessageMetadata> messageMetadata = new List <MessageMetadata>();

        if (physicalMessage.Headers.TryGetValue(Headers.EnclosedMessageTypes, out messageTypeIdentifier))
        {
            foreach (string messageTypeString in messageTypeIdentifier.Split(';'))
            {
                string          typeString = messageTypeString;
                MessageMetadata metadata   = messageMetadataRegistry.GetMessageMetadata(typeString);
                if (metadata == null)
                {
                    continue;
                }
                messageMetadata.Add(metadata);
            }

            if (messageMetadata.Count == 0 && physicalMessage.MessageIntent != MessageIntentEnum.Publish)
            {
                log.WarnFormat("Could not determine message type from message header '{0}'. MessageId: {1}", messageTypeIdentifier, physicalMessage.Id);
            }
        }

        return(Deserialize(physicalMessage, messageMetadata));
    }
Exemplo n.º 4
0
        private List <LogicalMessage> Extract(TransportMessage physicalMessage, string contentType)
        {
            if (physicalMessage.Body == null || physicalMessage.Body.Length == 0)
            {
                return(new List <LogicalMessage>());
            }

            string messageTypeIdentifier;
            var    messageMetadata = new List <MessageMetadata>();

            if (physicalMessage.Headers.TryGetValue(Headers.EnclosedMessageTypes, out messageTypeIdentifier))
            {
                foreach (string messageTypeString in messageTypeIdentifier.Split(';'))
                {
                    MessageMetadata metadata = _metadataRegistry.GetMessageMetadata(messageTypeString);
                    if (metadata == null)
                    {
                        continue;
                    }
                    messageMetadata.Add(metadata);
                }
            }

            using (var stream = new MemoryStream(physicalMessage.Body))
            {
                List <Type> messageTypesToDeserialize = messageMetadata.Select(metadata => metadata.MessageType).ToList();

                return(serializers[contentType].Deserialize(stream, messageTypesToDeserialize)
                       .Select(x => _logicalMessageFactory.Create(x.GetType(), x, physicalMessage.Headers))
                       .ToList());
            }
        }
 public void Should_return_metadata_for_a_mapped_type()
 {
     var defaultMessageRegistry = new MessageMetadataRegistry(false, new Conventions());
     defaultMessageRegistry.RegisterMessageType(typeof(int));
     var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(int));
     Assert.AreEqual(typeof(int), messageMetadata.MessageType);
     Assert.AreEqual(1, messageMetadata.MessageHierarchy.Count());
 }
Exemplo n.º 6
0
        public void Handle(IEvent @event)
        {
            var metadata = registry.GetMessageMetadata(@event.GetType());
            var context  = GlobalHost.ConnectionManager.GetConnectionContext <MessageStreamerConnection>();

            context.Connection.Broadcast(new Envelope {
                Types = metadata.MessageHierarchy.Select(t => t.Name).ToList(), Message = @event
            }, emptyArray)
            .Wait();
        }
Exemplo n.º 7
0
            public void Should_return_metadata_for_a_mapped_type()
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(false, new Conventions());

                defaultMessageRegistry.RegisterMessageType(typeof(int));
                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(int));

                Assert.AreEqual(typeof(int), messageMetadata.MessageType);
                Assert.AreEqual(1, messageMetadata.MessageHierarchy.Count());
            }
            public void Should_match_types_from_a_different_assembly(string typeName)
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(new Conventions().IsMessageType);

                defaultMessageRegistry.RegisterMessageTypesFoundIn(new List <Type> {
                    typeof(MyEvent)
                });

                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeName);

                Assert.AreEqual(typeof(MyEvent), messageMetadata.MessageHierarchy.ToList()[0]);
            }
            public void Should_return_metadata_for_a_mapped_type()
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(type => type == typeof(int));

                defaultMessageRegistry.RegisterMessageTypesFoundIn(new List <Type> {
                    typeof(int)
                });

                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(int));

                Assert.AreEqual(typeof(int), messageMetadata.MessageType);
                Assert.AreEqual(1, messageMetadata.MessageHierarchy.Count());
            }
Exemplo n.º 10
0
        string SerializeEnclosedMessageTypes(Type messageType)
        {
            var metadata = _messageMeta.GetMessageMetadata(messageType);

            var assemblyQualifiedNames = new HashSet <string>();

            foreach (var type in metadata.MessageHierarchy)
            {
                assemblyQualifiedNames.Add(type.AssemblyQualifiedName);
            }

            return(string.Join(";", assemblyQualifiedNames));
        }
Exemplo n.º 11
0
        public override Task Invoke(IIncomingPhysicalMessageContext context, Func <Task> next)
        {
            var messageType     = context.MessageHeaders[Headers.EnclosedMessageTypes];
            var messageMetadata = messageMetadataRegistry.GetMessageMetadata(messageType);

            if (messageMetadata == null)
            {
                var destination = passThroughRoutingRule(messageType);
                return(context.ForwardCurrentMessageTo(destination));
            }

            return(next());
        }
            public void Should_return_metadata_for_a_mapped_type()
            {
                var conventions = new Conventions();
                conventions.IsMessageTypeAction = type => type == typeof(int);

                var defaultMessageRegistry = new MessageMetadataRegistry(conventions);
                defaultMessageRegistry.RegisterMessageTypesFoundIn(new List<Type> { typeof(int) });

                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(int));

                Assert.AreEqual(typeof(int), messageMetadata.MessageType);
                Assert.AreEqual(1, messageMetadata.MessageHierarchy.Count());
            }
Exemplo n.º 13
0
            public void Should_return_the_correct_parent_hierarchy()
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(false, new Conventions());

                defaultMessageRegistry.RegisterMessageType(typeof(MyEvent));
                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(MyEvent));

                Assert.AreEqual(5, messageMetadata.MessageHierarchy.Count());

                Assert.AreEqual(typeof(MyEvent), messageMetadata.MessageHierarchy.ToList()[0]);
                Assert.AreEqual(typeof(InterfaceParent1), messageMetadata.MessageHierarchy.ToList()[1]);
                Assert.AreEqual(typeof(ConcreteParent1), messageMetadata.MessageHierarchy.ToList()[2]);
                Assert.AreEqual(typeof(InterfaceParent1Base), messageMetadata.MessageHierarchy.ToList()[3]);
                Assert.AreEqual(typeof(ConcreteParentBase), messageMetadata.MessageHierarchy.ToList()[4]);
            }
            public void Should_return_the_correct_parent_hierarchy()
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(new Conventions());

                defaultMessageRegistry.RegisterMessageTypesFoundIn(new List<Type> { typeof(MyEvent) });
                var messageMetadata = defaultMessageRegistry.GetMessageMetadata(typeof(MyEvent));

                Assert.AreEqual(5, messageMetadata.MessageHierarchy.Count());

                Assert.AreEqual(typeof(MyEvent), messageMetadata.MessageHierarchy.ToList()[0]);
                Assert.AreEqual(typeof(InterfaceParent1), messageMetadata.MessageHierarchy.ToList()[1]);
                Assert.AreEqual(typeof(ConcreteParent1), messageMetadata.MessageHierarchy.ToList()[2]);
                Assert.AreEqual(typeof(InterfaceParent1Base), messageMetadata.MessageHierarchy.ToList()[3]);
                Assert.AreEqual(typeof(ConcreteParentBase), messageMetadata.MessageHierarchy.ToList()[4]);

            }
Exemplo n.º 15
0
    Type[] BuildTypesPublishedWithInheritance(Type[] messageTypesPublished, MessageMetadataRegistry registry)
    {
        if (!messageTypesPublished.Any())
        {
            return(messageTypesPublished);
        }

        var publishedMessageTypes = new HashSet <Type>(messageTypesPublished);

        foreach (var t in messageTypesPublished)
        {
            publishedMessageTypes.UnionWith(registry.GetMessageMetadata(t).MessageHierarchy);
        }

        return(publishedMessageTypes.ToArray());
    }
Exemplo n.º 16
0
    List <LogicalMessage> Extract(IncomingMessage physicalMessage)
    {
        if (physicalMessage.Body == null || physicalMessage.Body.Length == 0)
        {
            return(new List <LogicalMessage>());
        }

        string messageTypeIdentifier;
        var    messageMetadata = new List <MessageMetadata>();

        var headers = physicalMessage.Headers;

        if (headers.TryGetValue(Headers.EnclosedMessageTypes, out messageTypeIdentifier))
        {
            foreach (var messageTypeString in messageTypeIdentifier.Split(';'))
            {
                var typeString = messageTypeString;
                var metadata   = messageMetadataRegistry.GetMessageMetadata(typeString);
                if (metadata == null)
                {
                    continue;
                }

                messageMetadata.Add(metadata);
            }

            if (
                messageMetadata.Count == 0 &&
                physicalMessage.GetMesssageIntent() != MessageIntentEnum.Publish)
            {
                log.Warn($"Could not determine message type from message header '{messageTypeIdentifier}'. MessageId: {physicalMessage.MessageId}");
            }
        }

        using (var stream = new MemoryStream(physicalMessage.Body))
        {
            var messageSerializer  = serializationMapper.GetSerializer(headers);
            var typesToDeserialize = messageMetadata
                                     .Select(metadata => metadata.MessageType)
                                     .ToList();
            return(messageSerializer.Deserialize(stream, typesToDeserialize)
                   .Select(x => logicalMessageFactory.Create(x.GetType(), x))
                   .ToList());
        }
    }
Exemplo n.º 17
0
        public void Invoke(OutgoingContext context, Action next)
        {
            var deliveryOptions = context.DeliveryOptions;

            var toSend = new TransportMessage {
                MessageIntent = MessageIntentEnum.Publish
            };

            var sendOptions = deliveryOptions as SendOptions;


            if (sendOptions != null)
            {
                toSend.MessageIntent = sendOptions is ReplyOptions ? MessageIntentEnum.Reply : MessageIntentEnum.Send;

                if (sendOptions.CorrelationId != null)
                {
                    toSend.CorrelationId = sendOptions.CorrelationId;
                }
            }

            //apply static headers
            foreach (var kvp in UnicastBus.OutgoingHeaders)
            {
                toSend.Headers[kvp.Key] = kvp.Value;
            }

            //apply individual headers
            foreach (var kvp in context.OutgoingLogicalMessage.Headers)
            {
                toSend.Headers[kvp.Key] = kvp.Value;
            }

            if (context.OutgoingLogicalMessage.MessageType != null)
            {
                var messageDefinitions = MessageMetadataRegistry.GetMessageMetadata(context.OutgoingLogicalMessage.MessageType);

                toSend.TimeToBeReceived = messageDefinitions.TimeToBeReceived;
                toSend.Recoverable      = messageDefinitions.Recoverable;
            }

            context.Set(toSend);

            next();
        }
Exemplo n.º 18
0
        private string SerializeEnclosedMessageTypes(Type messageType)
        {
            var metadata = _registry.GetMessageMetadata(messageType);

            var assemblyQualifiedNames = new List <string>(metadata.MessageHierarchy.Length);

            foreach (var type in metadata.MessageHierarchy)
            {
                var typeAssemblyQualifiedName = type.AssemblyQualifiedName;
                if (assemblyQualifiedNames.Contains(typeAssemblyQualifiedName))
                {
                    continue;
                }

                assemblyQualifiedNames.Add(typeAssemblyQualifiedName);
            }

            return(string.Join(";", assemblyQualifiedNames));
        }
Exemplo n.º 19
0
        public Type[] GetMessageHierarchy(Type messageType)
        {
            var metadata = _metadata.GetMessageMetadata(messageType);

            return(metadata.MessageHierarchy);
        }
 public void Should_throw_an_exception_for_a_unmapped_type()
 {
     var defaultMessageRegistry = new MessageMetadataRegistry(new Conventions());
     Assert.Throws<Exception>(() => defaultMessageRegistry.GetMessageMetadata(typeof(int)));
 }
            public void Should_throw_an_exception_for_a_unmapped_type()
            {
                var defaultMessageRegistry = new MessageMetadataRegistry(_ => false);

                Assert.Throws <Exception>(() => defaultMessageRegistry.GetMessageMetadata(typeof(int)));
            }
Exemplo n.º 22
0
        public IEnumerable <Type> GetMessageHierarchy(Type messageType)
        {
            var metadata = _metadata.GetMessageMetadata(messageType);

            return(metadata.MessageHierarchy);
        }