Ejemplo n.º 1
0
            protected override AmqpMessage CreateAmqpMessage()
            {
                AmqpMessage nullable = null;
                int         num      = this.eventDatas.Count <EventData>();

                if (this.eventDatas != null && num > 1)
                {
                    IList <Data> datas      = new List <Data>();
                    EventData    eventDatum = null;
                    foreach (EventData eventData in this.eventDatas)
                    {
                        if (eventDatum != null)
                        {
                            if (eventDatum.PartitionKey != eventData.PartitionKey)
                            {
                                throw Fx.Exception.AsError(new InvalidOperationException(SRClient.EventHubSendBatchMismatchPartitionKey(eventDatum.PartitionKey ?? "(null)", eventData.PartitionKey ?? "(null)")), null);
                            }
                            if (eventDatum.Publisher != eventData.Publisher)
                            {
                                throw Fx.Exception.AsError(new InvalidOperationException(SRClient.EventHubSendBatchMismatchPublisher(eventDatum.Publisher ?? "(null)", eventData.Publisher ?? "(null)")), null);
                            }
                        }
                        else
                        {
                            eventDatum = eventData;
                        }
                        AmqpMessage amqpMessage = eventData.ToAmqpMessage();
                        amqpMessage.Batchable = base.Batchable;
                        if ((int)(amqpMessage.Sections & (SectionFlag.DeliveryAnnotations | SectionFlag.MessageAnnotations | SectionFlag.Properties | SectionFlag.ApplicationProperties)) == 0 && (eventData.BodyStream == null || eventData.BodyStream == Stream.Null))
                        {
                            throw new InvalidOperationException(SRClient.CannotSendAnEmptyEvent(eventData.GetType().Name));
                        }
                        ArraySegment <byte> nums = MessageConverter.ReadStream(amqpMessage.ToStream());
                        datas.Add(new Data()
                        {
                            Value = nums
                        });
                    }
                    nullable               = AmqpMessage.Create(datas);
                    nullable.Batchable     = true;
                    nullable.MessageFormat = new uint?(-2147404032);
                    MessageConverter.UpdateAmqpMessageHeadersAndProperties(nullable, eventDatum, false);
                }
                else if (this.eventDatas != null && num == 1)
                {
                    EventData eventDatum1 = this.eventDatas.First <EventData>();
                    nullable           = eventDatum1.ToAmqpMessage();
                    nullable.Batchable = base.Batchable;
                    if ((int)(nullable.Sections & (SectionFlag.DeliveryAnnotations | SectionFlag.MessageAnnotations | SectionFlag.Properties | SectionFlag.ApplicationProperties)) == 0 && (eventDatum1.BodyStream == null || eventDatum1.BodyStream == Stream.Null))
                    {
                        throw new InvalidOperationException(SRClient.CannotSendAnEmptyEvent(eventDatum1.GetType().Name));
                    }
                }
                return(nullable);
            }
Ejemplo n.º 2
0
 private static void UpdateAmqpMessageHeadersAndProperties(BrokeredMessage source, AmqpMessage target, SerializationTarget serializationTarget)
 {
     MessageConverter.UpdateAmqpMessageHeadersAndProperties(target, source, false);
     BrokeredMessage.MessageMembers initializedMembers = source.InitializedMembers;
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.PrefilteredHeaders) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-prefiltered-headers"] = MessageConverter.ReadStream(source.PrefilteredHeaders);
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.PrefilteredProperties) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-prefiltered-properties"] = MessageConverter.ReadStream(source.PrefilteredProperties);
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.TransferDestination) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-transfer-destination"] = source.TransferDestination;
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.TransferSource) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-transfer-source"] = source.TransferSource;
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.TransferSequenceNumber) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-transfer-sn"]      = source.TransferSequenceNumber;
         target.DeliveryAnnotations.Map["x-opt-transfer-session"] = source.SessionId;
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.TransferHopCount) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-transfer-hop-count"] = source.TransferHopCount;
     }
     if ((int)(initializedMembers & BrokeredMessage.MessageMembers.TransferDestinationEntityId) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-transfer-resource"] = source.TransferDestinationResourceId;
     }
     if (serializationTarget == SerializationTarget.Communication && (int)(initializedMembers & BrokeredMessage.MessageMembers.LockToken) != 0)
     {
         target.DeliveryAnnotations.Map["x-opt-lock-token"]  = source.LockToken;
         target.MessageAnnotations.Map["x-opt-locked-until"] = source.LockedUntilUtc;
     }
 }
Ejemplo n.º 3
0
        public static bool TryGetAmqpObjectFromNetObject(object netObject, MappingType mappingType, out object amqpObject)
        {
            amqpObject = null;
            if (netObject == null)
            {
                return(false);
            }
            switch (SerializationUtilities.GetTypeId(netObject))
            {
            case PropertyValueType.Byte:
            case PropertyValueType.SByte:
            case PropertyValueType.Char:
            case PropertyValueType.Int16:
            case PropertyValueType.UInt16:
            case PropertyValueType.Int32:
            case PropertyValueType.UInt32:
            case PropertyValueType.Int64:
            case PropertyValueType.UInt64:
            case PropertyValueType.Single:
            case PropertyValueType.Double:
            case PropertyValueType.Decimal:
            case PropertyValueType.Boolean:
            case PropertyValueType.Guid:
            case PropertyValueType.String:
            case PropertyValueType.DateTime:
            {
                amqpObject = netObject;
                break;
            }

            case PropertyValueType.Uri:
            {
                amqpObject = new DescribedType((object)"com.microsoft:uri", ((Uri)netObject).AbsoluteUri);
                break;
            }

            case PropertyValueType.DateTimeOffset:
            {
                object         obj            = "com.microsoft:datetime-offset";
                DateTimeOffset dateTimeOffset = (DateTimeOffset)netObject;
                amqpObject = new DescribedType(obj, (object)dateTimeOffset.UtcTicks);
                break;
            }

            case PropertyValueType.TimeSpan:
            {
                object   obj1     = "com.microsoft:timespan";
                TimeSpan timeSpan = (TimeSpan)netObject;
                amqpObject = new DescribedType(obj1, (object)timeSpan.Ticks);
                break;
            }

            case PropertyValueType.Stream:
            {
                if (mappingType != MappingType.ApplicationProperty)
                {
                    break;
                }
                amqpObject = MessageConverter.ReadStream((Stream)netObject);
                break;
            }

            case PropertyValueType.Unknown:
            {
                if (!(netObject is Stream))
                {
                    if (mappingType == MappingType.ApplicationProperty)
                    {
                        throw Microsoft.ServiceBus.Messaging.FxTrace.Exception.AsError(new SerializationException(SRClient.FailedToSerializeUnsupportedType(netObject.GetType().FullName)), null);
                    }
                    if (netObject is byte[])
                    {
                        amqpObject = new ArraySegment <byte>((byte[])netObject);
                        break;
                    }
                    else if (!(netObject is IList))
                    {
                        if (!(netObject is IDictionary))
                        {
                            break;
                        }
                        amqpObject = new AmqpMap((IDictionary)netObject);
                        break;
                    }
                    else
                    {
                        amqpObject = netObject;
                        break;
                    }
                }
                else
                {
                    if (mappingType != MappingType.ApplicationProperty)
                    {
                        break;
                    }
                    amqpObject = MessageConverter.ReadStream((Stream)netObject);
                    break;
                }
            }
            }
            return(amqpObject != null);
        }