Ejemplo n.º 1
0
    public void Invoke(OutgoingContext context, Action next)
    {
        var transportMessage = context.OutgoingMessage;

        if (!transportMessage.IsControlMessage())
        {
            var logicalMessage  = context.OutgoingLogicalMessage;
            var messageInstance = logicalMessage.Instance;
            var messageType     = messageInstance.GetType();

            var messageSerializer = serializationMapper.GetSerializer(messageType);
            transportMessage.Body = Serialize(messageSerializer, messageInstance);

            Dictionary <string, string> transportHeaders = transportMessage.Headers;
            transportHeaders[Headers.ContentType]          = messageSerializer.ContentType;
            transportHeaders[Headers.EnclosedMessageTypes] = SerializeEnclosedMessageTypes(logicalMessage);

            foreach (KeyValuePair <string, string> headerEntry in logicalMessage.Headers)
            {
                transportHeaders[headerEntry.Key] = headerEntry.Value;
            }
        }

        next();
    }
Ejemplo n.º 2
0
    public override async Task Invoke(IOutgoingLogicalMessageContext context, Func <IOutgoingPhysicalMessageContext, Task> stage)
    {
        if (context.ShouldSkipSerialization())
        {
            var emptyMessageContext = this.CreateOutgoingPhysicalMessageContext(
                messageBody: new byte[0],
                routingStrategies: context.RoutingStrategies,
                sourceContext: context);
            await stage(emptyMessageContext)
            .ConfigureAwait(false);

            return;
        }

        var messageType       = context.Message.MessageType;
        var messageSerializer = serializationMapper.GetSerializer(messageType);
        var headers           = context.Headers;

        headers[Headers.ContentType]          = messageSerializer.ContentType;
        headers[Headers.EnclosedMessageTypes] = SerializeEnclosedMessageTypes(messageType);

        var array = Serialize(messageSerializer, context);
        var physicalMessageContext = this.CreateOutgoingPhysicalMessageContext(
            messageBody: array,
            routingStrategies: context.RoutingStrategies,
            sourceContext: context);

        await stage(physicalMessageContext)
        .ConfigureAwait(false);
    }
Ejemplo n.º 3
0
 void Serialize(OutgoingContext context)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         Type messageType = context.OutgoingLogicalMessage.Instance.GetType();
         IMessageSerializer messageSerializer = serializationMapper.GetSerializer(messageType);
         messageSerializer.Serialize(context.OutgoingLogicalMessage.Instance, ms);
         context.OutgoingMessage.Headers[Headers.ContentType] = messageSerializer.ContentType;
         context.OutgoingMessage.Body = ms.ToArray();
     }
 }
 List <LogicalMessage> Deserialize(TransportMessage physicalMessage, List <MessageMetadata> messageMetadata)
 {
     using (MemoryStream stream = new MemoryStream(physicalMessage.Body))
     {
         IMessageSerializer messageSerializer         = serializationMapper.GetSerializer(physicalMessage.Headers);
         List <Type>        messageTypesToDeserialize = messageMetadata.Select(metadata => metadata.MessageType).ToList();
         return(messageSerializer.Deserialize(stream, messageTypesToDeserialize)
                .Select(x => logicalMessageFactory.Create(x.GetType(), x, physicalMessage.Headers))
                .ToList());
     }
 }
Ejemplo n.º 5
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());
        }
    }
    public override async Task Invoke(IOutgoingLogicalMessageContext context, Func <IOutgoingPhysicalMessageContext, Task> next)
    {
        if (context.ShouldSkipSerialization())
        {
            await next(new OutgoingPhysicalMessageContext(new byte[0], context.RoutingStrategies, context)).ConfigureAwait(false);

            return;
        }

        Type messageType = context.Message.MessageType;
        IMessageSerializer messageSerializer = serializationMapper.GetSerializer(messageType);

        context.Headers[Headers.ContentType]          = messageSerializer.ContentType;
        context.Headers[Headers.EnclosedMessageTypes] = SerializeEnclosedMessageTypes(messageType);

        var array = Serialize(messageSerializer, context);

        await next(new OutgoingPhysicalMessageContext(array, context.RoutingStrategies, context)).ConfigureAwait(false);
    }