Exemplo n.º 1
0
        public byte[] SaveEnvelopeData(ImmutableEnvelope envelope)
        {
            //  string contract, Guid messageId, Uri sender,
            var itemContracts = new MessageContract[envelope.Items.Length];

            using (var content = new MemoryStream())
            {
                int position = 0;
                for (int i = 0; i < envelope.Items.Length; i++)
                {
                    var item = envelope.Items[i];

                    string name;
                    if (!_dataSerializer.TryGetContractNameByType(item.MappedType, out name))
                    {
                        var error = string.Format("Failed to find contract name to serialize '{0}'.", item.MappedType);
                        throw new InvalidOperationException(error);
                    }
                    // normal serializers have a nasty habbit of closing the stream after they are done
                    // we can suppress that or use a wrapper now instead
                    using (var itemStream = new MemoryStream())
                    {
                        _dataSerializer.Serialize(item.Content, item.MappedType, itemStream);
                        var bytes = itemStream.ToArray();
                        content.Write(bytes, 0, bytes.Length);
                    }


                    int size            = (int)content.Position - position;
                    var attribContracts = EnvelopeConvert.ItemAttributesToContract(item.GetAllAttributes());
                    itemContracts[i] = new MessageContract(name, size, position, attribContracts);

                    position += size;
                }

                var envelopeAttribs = EnvelopeConvert.EnvelopeAttributesToContract(envelope.GetAllAttributes());


                var contract = new EnvelopeContract(envelope.EnvelopeId, envelopeAttribs, itemContracts,
                                                    envelope.DeliverOnUtc, envelope.CreatedOnUtc);

                using (var stream = new MemoryStream())
                {
                    // skip header
                    stream.Seek(EnvelopeHeaderContract.FixedSize, SeekOrigin.Begin);
                    // save envelope attributes
                    _envelopeSerializer.SerializeEnvelope(stream, contract);
                    var envelopeBytes = stream.Position - EnvelopeHeaderContract.FixedSize;
                    // copy data
                    content.WriteTo(stream);
                    // write the header
                    stream.Seek(0, SeekOrigin.Begin);
                    var header = new EnvelopeHeaderContract(EnvelopeHeaderContract.Schema2DataFormat, envelopeBytes, 0);
                    header.WriteToStream(stream);
                    return(stream.ToArray());
                }
            }
        }
Exemplo n.º 2
0
        public void SendEnvelope(ImmutableEnvelope envelope)
        {
            var queue = GetOutboundQueue();
            var data = _streamer.SaveEnvelopeData(envelope);

            if (Transaction.Current == null)
            {
                queue.PutMessage(data);

                SystemObserver.Notify(new EnvelopeSent(queue.Name, envelope.EnvelopeId, false,
                    envelope.Items.Select(x => x.MappedType.Name).ToArray(), envelope.GetAllAttributes()));
            }
            else
            {
                var action = new CommitActionEnlistment(() =>
                    {
                        queue.PutMessage(data);
                        SystemObserver.Notify(new EnvelopeSent(queue.Name, envelope.EnvelopeId, true,
                            envelope.Items.Select(x => x.MappedType.Name).ToArray(), envelope.GetAllAttributes()));
                    });
                Transaction.Current.EnlistVolatile(action, EnlistmentOptions.None);
            }
        }
Exemplo n.º 3
0
        public static EnvelopeBuilder CloneProperties(string newId, ImmutableEnvelope envelope)
        {
            if (newId == envelope.EnvelopeId)
            {
                throw new InvalidOperationException("Envelope cloned for modification should have new identity.");
            }
            var builder = new EnvelopeBuilder(newId);

            builder.OverrideCreatedOnUtc(envelope.CreatedOnUtc);
            builder.DeliverOnUtc(envelope.DeliverOnUtc);

            foreach (var attribute in envelope.GetAllAttributes())
            {
                builder.AddString(attribute.Key, attribute.Value);
            }
            return(builder);
        }
Exemplo n.º 4
0
        public static void PrintTo(this ImmutableEnvelope envelope, TextWriter writer, Func <object, string> serializer)
        {
            //.AppendFormat("{0,12}: {1}", attribute.GetName(), attribute.GetValue())
            writer.WriteLine(string.Format("{0,12}: {1}", "EnvelopeId", envelope.EnvelopeId));
            writer.WriteLine(string.Format("{0,12}: {1:yyyy-MM-dd HH:mm:ss} (UTC)", "Created", envelope.CreatedOnUtc));
            if (envelope.DeliverOnUtc != DateTime.MinValue)
            {
                writer.WriteLine(string.Format("{0,12}: {1:yyyy-MM-dd HH:mm:ss} (UTC)", "Deliver On",
                                               envelope.DeliverOnUtc));
            }

            foreach (var attribute in envelope.GetAllAttributes())
            {
                writer.WriteLine(string.Format("{0,12}: {1}", attribute.Key, attribute.Value));
            }

            foreach (var message in envelope.Items)
            {
                writer.WriteLine();
                writer.WriteLine("{0}. {1}", message.Index, message.MappedType);

                foreach (var attribute in message.GetAllAttributes())
                {
                    writer.WriteLine(string.Format("{0,12}: {1}", attribute.Key, attribute.Value));
                }

                try
                {
                    var buffer = serializer(message.Content);
                    writer.WriteLine(buffer);
                }
                catch (Exception ex)
                {
                    writer.WriteLine("Rendering failure");
                    writer.WriteLine(ex);
                }

                writer.WriteLine();
            }
        }
Exemplo n.º 5
0
        public static IEnumerable <ImmutableEnvelope> SelectMany(ImmutableEnvelope source)
        {
            if (source.Items.Length == 0)
            {
                yield break;
            }

            if (source.Items.Length == 1)
            {
                yield return(source);

                yield break;
            }

            for (int si = 0; si < source.Items.Length; si++)
            {
                var id = source.EnvelopeId + "-s" + si;
                yield return
                    (new ImmutableEnvelope(id, source.GetAllAttributes().ToArray(), new[] { source.Items[si] },
                                           source.DeliverOnUtc, source.CreatedOnUtc));
            }
        }
Exemplo n.º 6
0
        public byte[] SaveEnvelopeData(ImmutableEnvelope envelope)
        {
            //  string contract, Guid messageId, Uri sender,
            var itemContracts = new MessageContract[envelope.Items.Length];
            using (var content = new MemoryStream())
            {
                int position = 0;
                for (int i = 0; i < envelope.Items.Length; i++)
                {
                    var item = envelope.Items[i];

                    string name;
                    if (!_dataSerializer.TryGetContractNameByType(item.MappedType, out name))
                    {
                        var error = string.Format("Failed to find contract name to serialize '{0}'.", item.MappedType);
                        throw new InvalidOperationException(error);
                    }
                    // normal serializers have a nasty habbit of closing the stream after they are done
                    // we can suppress that or use a wrapper now instead
                    using (var itemStream = new MemoryStream())
                    {
                        _dataSerializer.Serialize(item.Content, item.MappedType, itemStream);
                        var bytes = itemStream.ToArray();
                        content.Write(bytes, 0, bytes.Length);
                    }

                    int size = (int) content.Position - position;
                    var attribContracts = EnvelopeConvert.ItemAttributesToContract(item.GetAllAttributes());
                    itemContracts[i] = new MessageContract(name, size, position, attribContracts);

                    position += size;
                }

                var envelopeAttribs = EnvelopeConvert.EnvelopeAttributesToContract(envelope.GetAllAttributes());

                var contract = new EnvelopeContract(envelope.EnvelopeId, envelopeAttribs, itemContracts,
                    envelope.DeliverOnUtc, envelope.CreatedOnUtc);

                using (var stream = new MemoryStream())
                {
                    // skip header
                    stream.Seek(EnvelopeHeaderContract.FixedSize, SeekOrigin.Begin);
                    // save envelope attributes
                    _envelopeSerializer.SerializeEnvelope(stream, contract);
                    var envelopeBytes = stream.Position - EnvelopeHeaderContract.FixedSize;
                    // copy data
                    content.WriteTo(stream);
                    // write the header
                    stream.Seek(0, SeekOrigin.Begin);
                    var header = new EnvelopeHeaderContract(EnvelopeHeaderContract.Schema2DataFormat, envelopeBytes, 0);
                    header.WriteToStream(stream);
                    return stream.ToArray();
                }
            }
        }
Exemplo n.º 7
0
        public void SendEnvelope(ImmutableEnvelope envelope)
        {
            var queue = GetOutboundQueue();
            var data  = _streamer.SaveEnvelopeData(envelope);

            if (Transaction.Current == null)
            {
                queue.PutMessage(data);

                SystemObserver.Notify(new EnvelopeSent(queue.Name, envelope.EnvelopeId, false,
                                                       envelope.Items.Select(x => x.MappedType.Name).ToArray(), envelope.GetAllAttributes()));
            }
            else
            {
                var action = new CommitActionEnlistment(() =>
                {
                    queue.PutMessage(data);
                    SystemObserver.Notify(new EnvelopeSent(queue.Name, envelope.EnvelopeId, true,
                                                           envelope.Items.Select(x => x.MappedType.Name).ToArray(), envelope.GetAllAttributes()));
                });
                Transaction.Current.EnlistVolatile(action, EnlistmentOptions.None);
            }
        }