Exemplo n.º 1
0
 public void SetMessageProperties(MsmqIntegrationMessageProperty property)
 {
     property.AcknowledgeType     = new AcknowledgeTypes?((AcknowledgeTypes)this.acknowledge.Value);
     property.Acknowledgment      = new Acknowledgment?((Acknowledgment)base.Class.Value);
     property.AdministrationQueue = GetQueueName(this.adminQueue.GetValue(this.adminQueueLength.Value));
     property.AppSpecific         = new int?(this.appSpecific.Value);
     property.ArrivedTime         = new DateTime?(MsmqDateTime.ToDateTime(this.arrivedTime.Value).ToLocalTime());
     property.Authenticated       = new bool?(this.authenticated.Value != 0);
     property.BodyType            = new int?(this.bodyType.Value);
     property.CorrelationId       = MsmqMessageId.ToString(this.correlationId.Buffer);
     property.DestinationQueue    = GetQueueName(this.destinationQueue.GetValue(this.destinationQueueLength.Value));
     property.Extension           = this.extension.GetBufferCopy(this.extensionLength.Value);
     property.Id    = MsmqMessageId.ToString(base.MessageId.Buffer);
     property.Label = this.label.GetValue(this.labelLength.Value);
     if (base.Class.Value == 0)
     {
         property.MessageType = 2;
     }
     else if (base.Class.Value == 1)
     {
         property.MessageType = 3;
     }
     else
     {
         property.MessageType = 1;
     }
     property.Priority      = new MessagePriority?((MessagePriority)this.priority.Value);
     property.ResponseQueue = GetQueueName(this.responseFormatName.GetValue(this.responseFormatNameLength.Value));
     property.SenderId      = base.SenderId.GetBufferCopy(base.SenderIdLength.Value);
     property.SentTime      = new DateTime?(MsmqDateTime.ToDateTime(this.sentTime.Value).ToLocalTime());
     property.InternalSetTimeToReachQueue(MsmqDuration.ToTimeSpan(this.timeToReachQueue.Value));
 }
Exemplo n.º 2
0
 public MsmqMessage(T body)
 {
     if (body == null)
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("body");
     this.property = new MsmqIntegrationMessageProperty();
     this.property.Body = body;
 }
        public void SetMessageProperties(MsmqIntegrationMessageProperty property)
        {
            property.AcknowledgeType     = (System.Messaging.AcknowledgeTypes) this.acknowledge.Value;
            property.Acknowledgment      = (System.Messaging.Acknowledgment) this.Class.Value;
            property.AdministrationQueue = GetQueueName(this.adminQueue.GetValue(this.adminQueueLength.Value));
            property.AppSpecific         = this.appSpecific.Value;
            property.ArrivedTime         = MsmqDateTime.ToDateTime(this.arrivedTime.Value).ToLocalTime();
            property.Authenticated       = this.authenticated.Value != 0;
            property.BodyType            = this.bodyType.Value;
            property.CorrelationId       = MsmqMessageId.ToString(this.correlationId.Buffer);
            property.DestinationQueue    = GetQueueName(this.destinationQueue.GetValue(this.destinationQueueLength.Value));
            property.Extension           = this.extension.GetBufferCopy(this.extensionLength.Value);
            property.Id    = MsmqMessageId.ToString(this.MessageId.Buffer);
            property.Label = this.label.GetValue(this.labelLength.Value);

            if (this.Class.Value == UnsafeNativeMethods.MQMSG_CLASS_NORMAL)
            {
                property.MessageType = System.Messaging.MessageType.Normal;
            }
            else if (this.Class.Value == UnsafeNativeMethods.MQMSG_CLASS_REPORT)
            {
                property.MessageType = System.Messaging.MessageType.Report;
            }
            else
            {
                property.MessageType = System.Messaging.MessageType.Acknowledgment;
            }

            property.Priority      = (System.Messaging.MessagePriority) this.priority.Value;
            property.ResponseQueue = GetQueueName(this.responseFormatName.GetValue(this.responseFormatNameLength.Value));
            property.SenderId      = this.SenderId.GetBufferCopy(this.SenderIdLength.Value);
            property.SentTime      = MsmqDateTime.ToDateTime(this.sentTime.Value).ToLocalTime();
            property.InternalSetTimeToReachQueue(MsmqDuration.ToTimeSpan(this.timeToReachQueue.Value));
        }
        public void SetMessageProperties(MsmqIntegrationMessageProperty property)
        {
            property.AcknowledgeType = (System.Messaging.AcknowledgeTypes)this.acknowledge.Value;
            property.Acknowledgment = (System.Messaging.Acknowledgment)this.Class.Value;
            property.AdministrationQueue = GetQueueName(this.adminQueue.GetValue(this.adminQueueLength.Value));
            property.AppSpecific = this.appSpecific.Value;
            property.ArrivedTime = MsmqDateTime.ToDateTime(this.arrivedTime.Value).ToLocalTime();
            property.Authenticated = this.authenticated.Value != 0;
            property.BodyType = this.bodyType.Value;
            property.CorrelationId = MsmqMessageId.ToString(this.correlationId.Buffer);
            property.DestinationQueue = GetQueueName(this.destinationQueue.GetValue(this.destinationQueueLength.Value));
            property.Extension = this.extension.GetBufferCopy(this.extensionLength.Value);
            property.Id = MsmqMessageId.ToString(this.MessageId.Buffer);
            property.Label = this.label.GetValue(this.labelLength.Value);

            if (this.Class.Value == UnsafeNativeMethods.MQMSG_CLASS_NORMAL)
                property.MessageType = System.Messaging.MessageType.Normal;
            else if (this.Class.Value == UnsafeNativeMethods.MQMSG_CLASS_REPORT)
                property.MessageType = System.Messaging.MessageType.Report;
            else
                property.MessageType = System.Messaging.MessageType.Acknowledgment;

            property.Priority = (System.Messaging.MessagePriority)this.priority.Value;
            property.ResponseQueue = GetQueueName(this.responseFormatName.GetValue(this.responseFormatNameLength.Value));
            property.SenderId = this.SenderId.GetBufferCopy(this.SenderIdLength.Value);
            property.SentTime = MsmqDateTime.ToDateTime(this.sentTime.Value).ToLocalTime();
            property.InternalSetTimeToReachQueue(MsmqDuration.ToTimeSpan(this.timeToReachQueue.Value));
        }
Exemplo n.º 5
0
        //
        // Returns stream containing serialized body
        // In case of MsmqMessageSerializationFormat.Stream,
        // returns body as Stream, and throws exception if body is not a Stream
        //
        internal Stream Serialize(MsmqIntegrationMessageProperty property)
        {
            Stream stream;

            switch (this.SerializationFormat)
            {
            case MsmqMessageSerializationFormat.Xml:
                stream = new MemoryStream();
                XmlSerializer serializer = GetXmlSerializerForType(property.Body.GetType());
                serializer.Serialize(stream, property.Body);
                return(stream);

            case MsmqMessageSerializationFormat.Binary:
                stream = new MemoryStream();
                BinaryFormatter.Serialize(stream, property.Body);
                // need to set BodyType to a magic number used by System.Messaging
                property.BodyType = 0x300;
                return(stream);

            case MsmqMessageSerializationFormat.ActiveX:
                if (property.BodyType.HasValue)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.MsmqCannotUseBodyTypeWithActiveXSerialization)));
                }

                stream = new MemoryStream();
                int bodyType = 0;
                ActiveXSerializer.Serialize(stream as MemoryStream, property.Body, ref bodyType);
                property.BodyType = bodyType;
                return(stream);

            case MsmqMessageSerializationFormat.ByteArray:
                // body MUST be byte array
                byte[] byteArray = property.Body as byte[];
                if (byteArray == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.MsmqByteArrayBodyExpected)));
                }

                stream = new MemoryStream();
                stream.Write(byteArray, 0, byteArray.Length);
                return(stream);

            case MsmqMessageSerializationFormat.Stream:
                // body MUST be a stream
                Stream bodyStream = property.Body as Stream;
                if (bodyStream == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.MsmqStreamBodyExpected)));
                }

                // NOTE: we don't copy here as a perf optimization, but this might be dangerous
                return(bodyStream);

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.GetString(SR.MsmqUnsupportedSerializationFormat, this.SerializationFormat)));
            }
        }
 internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
 {
     Message message2;
     using (MsmqDiagnostics.BoundReceiveBytesOperation())
     {
         Message message = Message.CreateMessage(MessageVersion.None, (string) null);
         bool flag = true;
         try
         {
             SecurityMessageProperty property = listener.ValidateSecurity(msmqMessage);
             if (property != null)
             {
                 message.Properties.Security = property;
             }
             MsmqIntegrationMessageProperty property2 = new MsmqIntegrationMessageProperty();
             msmqMessage.SetMessageProperties(property2);
             int length = msmqMessage.BodyLength.Value;
             if (length > listener.MaxReceivedMessageSize)
             {
                 receiver.FinalDisposition(messageProperty);
                 throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
             }
             byte[] bufferCopy = msmqMessage.Body.GetBufferCopy(length);
             MemoryStream bodyStream = new MemoryStream(bufferCopy, 0, bufferCopy.Length, false);
             object obj2 = null;
             using (MsmqDiagnostics.BoundDecodeOperation())
             {
                 try
                 {
                     obj2 = DeserializeForIntegration(listener, bodyStream, property2, messageProperty.LookupId);
                 }
                 catch (SerializationException exception)
                 {
                     receiver.FinalDisposition(messageProperty);
                     throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(System.ServiceModel.SR.GetString("MsmqDeserializationError"), exception));
                 }
                 property2.Body = obj2;
                 message.Properties["MsmqIntegrationMessageProperty"] = property2;
                 bodyStream.Seek(0L, SeekOrigin.Begin);
                 message.Headers.To = listener.Uri;
                 flag = false;
                 MsmqDiagnostics.TransferFromTransport(message);
             }
             message2 = message;
         }
         finally
         {
             if (flag)
             {
                 message.Close();
             }
         }
     }
     return message2;
 }
Exemplo n.º 7
0
        internal Stream Serialize(MsmqIntegrationMessageProperty property)
        {
            Stream stream;

            switch (this.SerializationFormat)
            {
            case MsmqMessageSerializationFormat.Xml:
                stream = new MemoryStream();
                this.GetXmlSerializerForType(property.Body.GetType()).Serialize(stream, property.Body);
                return(stream);

            case MsmqMessageSerializationFormat.Binary:
                stream = new MemoryStream();
                this.BinaryFormatter.Serialize(stream, property.Body);
                property.BodyType = 0x300;
                return(stream);

            case MsmqMessageSerializationFormat.ActiveX:
            {
                if (property.BodyType.HasValue)
                {
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(System.ServiceModel.SR.GetString("MsmqCannotUseBodyTypeWithActiveXSerialization")));
                }
                stream = new MemoryStream();
                int bodyType = 0;
                this.ActiveXSerializer.Serialize(stream as MemoryStream, property.Body, ref bodyType);
                property.BodyType = new int?(bodyType);
                return(stream);
            }

            case MsmqMessageSerializationFormat.ByteArray:
            {
                byte[] body = property.Body as byte[];
                if (body == null)
                {
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(System.ServiceModel.SR.GetString("MsmqByteArrayBodyExpected")));
                }
                stream = new MemoryStream();
                stream.Write(body, 0, body.Length);
                return(stream);
            }

            case MsmqMessageSerializationFormat.Stream:
            {
                Stream stream2 = property.Body as Stream;
                if (stream2 == null)
                {
                    throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(System.ServiceModel.SR.GetString("MsmqStreamBodyExpected")));
                }
                return(stream2);
            }
            }
            throw System.ServiceModel.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(System.ServiceModel.SR.GetString("MsmqUnsupportedSerializationFormat", new object[] { this.SerializationFormat })));
        }
        // not verified / consider queue property filter
        internal static MsmqIntegrationMessageProperty Get(MQMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }
            var p = new MsmqIntegrationMessageProperty();

            p.Id            = message.Id;
            p.Label         = message.Label;
            p.CorrelationId = message.CorrelationId;
            p.Body          = message.Body;
            p.Extension     = message.Extension;
            p.SenderId      = message.SenderId;

            p.DestinationQueue = CreateUriFromQueue(message.DestinationQueue);
            if (message.AdministrationQueue != null)
            {
                p.AdministrationQueue = CreateUriFromQueue(message.AdministrationQueue);
            }
            if (message.ResponseQueue != null)
            {
                p.ResponseQueue = CreateUriFromQueue(message.ResponseQueue);
            }

            // FIXME: check property filter in the queue
            p.AppSpecific      = message.AppSpecific;
            p.ArrivedTime      = message.ArrivedTime;
            p.Authenticated    = message.Authenticated;
            p.Priority         = message.Priority;
            p.SentTime         = message.SentTime;
            p.TimeToReachQueue = message.TimeToReachQueue;

            switch (message.MessageType)
            {
            case MQMessageType.Acknowledgment:
                p.AcknowledgeType = message.AcknowledgeType;
                p.Acknowledgment  = message.Acknowledgment;
                break;

            case MQMessageType.Normal:
            case MQMessageType.Report:
                // anything to do?
                break;
            }

            return(p);
        }
		// not verified / consider queue property filter
		internal static MsmqIntegrationMessageProperty Get (MQMessage message)
		{
			if (message == null)
				throw new ArgumentNullException ("message");
			var p = new MsmqIntegrationMessageProperty ();
			p.Id = message.Id;
			p.Label = message.Label;
			p.CorrelationId = message.CorrelationId;
			p.Body = message.Body;
			p.Extension = message.Extension;
			p.SenderId = message.SenderId;

			p.DestinationQueue = CreateUriFromQueue (message.DestinationQueue);
			if (message.AdministrationQueue != null)
				p.AdministrationQueue = CreateUriFromQueue (message.AdministrationQueue);
			if (message.ResponseQueue != null)
				p.ResponseQueue = CreateUriFromQueue (message.ResponseQueue);

			// FIXME: check property filter in the queue
			p.AppSpecific = message.AppSpecific;
			p.ArrivedTime = message.ArrivedTime;
			p.Authenticated = message.Authenticated;
			p.Priority = message.Priority;
			p.SentTime = message.SentTime;
			p.TimeToReachQueue = message.TimeToReachQueue;

			switch (message.MessageType) {
			case MQMessageType.Acknowledgment:
				p.AcknowledgeType = message.AcknowledgeType;
				p.Acknowledgment = message.Acknowledgment;
				break;
			case MQMessageType.Normal:
			case MQMessageType.Report:
				// anything to do?
				break;
			}

			return p;
		}
 public MsmqIntegrationOutputMessage(MsmqChannelFactoryBase <IOutputChannel> factory, int bodySize, EndpointAddress remoteAddress, MsmqIntegrationMessageProperty property) : base(factory, bodySize, remoteAddress, 8)
 {
     if (property.AcknowledgeType.HasValue)
     {
         this.EnsureAcknowledgeProperty((byte)property.AcknowledgeType.Value);
     }
     if (null != property.AdministrationQueue)
     {
         this.EnsureAdminQueueProperty(property.AdministrationQueue, false);
     }
     if (property.AppSpecific.HasValue)
     {
         this.appSpecific = new NativeMsmqMessage.IntProperty(this, 8, property.AppSpecific.Value);
     }
     if (property.BodyType.HasValue)
     {
         base.EnsureBodyTypeProperty(property.BodyType.Value);
     }
     if (property.CorrelationId != null)
     {
         this.correlationId = new NativeMsmqMessage.BufferProperty(this, 3, MsmqMessageId.FromString(property.CorrelationId));
     }
     if (property.Extension != null)
     {
         this.extension = new NativeMsmqMessage.BufferProperty(this, 0x23, property.Extension);
     }
     if (property.Label != null)
     {
         this.label = new NativeMsmqMessage.StringProperty(this, 11, property.Label);
     }
     if (property.Priority.HasValue)
     {
         this.priority = new NativeMsmqMessage.ByteProperty(this, 4, (byte)property.Priority.Value);
     }
     if (null != property.ResponseQueue)
     {
         this.EnsureResponseQueueProperty(property.ResponseQueue);
     }
     if (property.TimeToReachQueue.HasValue)
     {
         base.EnsureTimeToReachQueueProperty(MsmqDuration.FromTimeSpan(property.TimeToReachQueue.Value));
     }
 }
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            int length;
            MessageProperties properties            = message.Properties;
            Stream            stream                = null;
            MsmqIntegrationMessageProperty property = MsmqIntegrationMessageProperty.Get(message);

            if (property == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(System.ServiceModel.SR.GetString("MsmqMessageDoesntHaveIntegrationProperty")));
            }
            if (property.Body != null)
            {
                stream = this.factory.Serialize(property);
            }
            if (stream == null)
            {
                length = 0;
            }
            else
            {
                if (stream.Length > 0x7fffffffL)
                {
                    throw TraceUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("MessageSizeMustBeInIntegerRange")), message);
                }
                length = (int)stream.Length;
            }
            using (MsmqIntegrationOutputMessage message2 = new MsmqIntegrationOutputMessage(this.factory, length, this.RemoteAddress, property))
            {
                message2.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);
                if (stream != null)
                {
                    int num3;
                    stream.Position = 0L;
                    for (int i = length; i > 0; i -= num3)
                    {
                        num3 = stream.Read(message2.Body.Buffer, 0, i);
                    }
                }
                bool lockHeld = false;
                try
                {
                    Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                    this.msmqQueue.Send(message2, this.transactionMode);
                    MsmqDiagnostics.DatagramSent(message2.MessageId, message);
                    property.Id = MsmqMessageId.ToString(message2.MessageId.Buffer);
                }
                catch (MsmqException exception)
                {
                    if (exception.FaultSender)
                    {
                        base.Fault();
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception.Normalized);
                }
                finally
                {
                    if (lockHeld)
                    {
                        Msmq.LeaveXPSendLock();
                    }
                }
            }
        }
Exemplo n.º 12
0
 public MsmqMessage(T body)
 {
     prop = new MsmqIntegrationMessageProperty();
     Body = body;
 }
        private static object DeserializeForIntegration(MsmqIntegrationChannelListener listener, Stream bodyStream, MsmqIntegrationMessageProperty property, long lookupId)
        {
            MsmqMessageSerializationFormat serializationFormat = (listener.ReceiveParameters as MsmqIntegrationReceiveParameters).SerializationFormat;
            switch (serializationFormat)
            {
                case MsmqMessageSerializationFormat.Xml:
                    return XmlDeserializeForIntegration(listener, bodyStream, lookupId);

                case MsmqMessageSerializationFormat.Binary:
                    return BinaryFormatter.Deserialize(bodyStream);

                case MsmqMessageSerializationFormat.ActiveX:
                {
                    int bodyType = property.BodyType.Value;
                    return ActiveXSerializer.Deserialize(bodyStream as MemoryStream, bodyType);
                }
                case MsmqMessageSerializationFormat.ByteArray:
                    return (bodyStream as MemoryStream).ToArray();

                case MsmqMessageSerializationFormat.Stream:
                    return bodyStream;
            }
            throw new SerializationException(System.ServiceModel.SR.GetString("MsmqUnsupportedSerializationFormat", new object[] { serializationFormat }));
        }
 public void SetMessageProperties(MsmqIntegrationMessageProperty property)
 {
     property.AcknowledgeType = new AcknowledgeTypes?((AcknowledgeTypes) this.acknowledge.Value);
     property.Acknowledgment = new Acknowledgment?((Acknowledgment) base.Class.Value);
     property.AdministrationQueue = GetQueueName(this.adminQueue.GetValue(this.adminQueueLength.Value));
     property.AppSpecific = new int?(this.appSpecific.Value);
     property.ArrivedTime = new DateTime?(MsmqDateTime.ToDateTime(this.arrivedTime.Value).ToLocalTime());
     property.Authenticated = new bool?(this.authenticated.Value != 0);
     property.BodyType = new int?(this.bodyType.Value);
     property.CorrelationId = MsmqMessageId.ToString(this.correlationId.Buffer);
     property.DestinationQueue = GetQueueName(this.destinationQueue.GetValue(this.destinationQueueLength.Value));
     property.Extension = this.extension.GetBufferCopy(this.extensionLength.Value);
     property.Id = MsmqMessageId.ToString(base.MessageId.Buffer);
     property.Label = this.label.GetValue(this.labelLength.Value);
     if (base.Class.Value == 0)
     {
         property.MessageType = 2;
     }
     else if (base.Class.Value == 1)
     {
         property.MessageType = 3;
     }
     else
     {
         property.MessageType = 1;
     }
     property.Priority = new MessagePriority?((MessagePriority) this.priority.Value);
     property.ResponseQueue = GetQueueName(this.responseFormatName.GetValue(this.responseFormatNameLength.Value));
     property.SenderId = base.SenderId.GetBufferCopy(base.SenderIdLength.Value);
     property.SentTime = new DateTime?(MsmqDateTime.ToDateTime(this.sentTime.Value).ToLocalTime());
     property.InternalSetTimeToReachQueue(MsmqDuration.ToTimeSpan(this.timeToReachQueue.Value));
 }
        internal static Message DecodeIntegrationDatagram(MsmqIntegrationChannelListener listener, MsmqReceiveHelper receiver, MsmqIntegrationInputMessage msmqMessage, MsmqMessageProperty messageProperty)
        {
            using (MsmqDiagnostics.BoundReceiveBytesOperation())
            {
                Message message = Message.CreateMessage(MessageVersion.None, (string)null);
                bool closeMessage = true;

                try
                {
                    SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage);
                    if (null != securityProperty)
                        message.Properties.Security = securityProperty;

                    MsmqIntegrationMessageProperty integrationProperty = new MsmqIntegrationMessageProperty();
                    msmqMessage.SetMessageProperties(integrationProperty);

                    int size = msmqMessage.BodyLength.Value;

                    if (size > listener.MaxReceivedMessageSize)
                    {
                        receiver.FinalDisposition(messageProperty);
                        throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize));
                    }

                    byte[] bodyBytes = msmqMessage.Body.GetBufferCopy(size);

                    MemoryStream bodyStream = new MemoryStream(bodyBytes, 0, bodyBytes.Length, false);

                    object body = null;
                    using (MsmqDiagnostics.BoundDecodeOperation())
                    {
                        try
                        {
                            body = DeserializeForIntegration(listener, bodyStream, integrationProperty, messageProperty.LookupId);
                        }
                        catch (SerializationException e)
                        {
                            receiver.FinalDisposition(messageProperty);
                            throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqDeserializationError), e));
                        }

                        integrationProperty.Body = body;
                        message.Properties[MsmqIntegrationMessageProperty.Name] = integrationProperty;
                        bodyStream.Seek(0, SeekOrigin.Begin);
                        message.Headers.To = listener.Uri;
                        closeMessage = false;
                        MsmqDiagnostics.TransferFromTransport(message);
                    }
                    return message;
                }
                finally
                {
                    if (closeMessage)
                        message.Close();
                }
            }
        }
            public MsmqIntegrationOutputMessage(
                MsmqChannelFactoryBase <IOutputChannel> factory,
                int bodySize,
                EndpointAddress remoteAddress,
                MsmqIntegrationMessageProperty property)
                : base(factory, bodySize, remoteAddress, 8)
            {
                if (null == property)
                {
                    Fx.Assert("MsmqIntegrationMessageProperty expected");
                }

                if (property.AcknowledgeType.HasValue)
                {
                    EnsureAcknowledgeProperty((byte)property.AcknowledgeType.Value);
                }

                if (null != property.AdministrationQueue)
                {
                    EnsureAdminQueueProperty(property.AdministrationQueue, false);
                }

                if (property.AppSpecific.HasValue)
                {
                    this.appSpecific = new IntProperty(this, UnsafeNativeMethods.PROPID_M_APPSPECIFIC, property.AppSpecific.Value);
                }

                if (property.BodyType.HasValue)
                {
                    EnsureBodyTypeProperty(property.BodyType.Value);
                }

                if (null != property.CorrelationId)
                {
                    this.correlationId = new BufferProperty(this, UnsafeNativeMethods.PROPID_M_CORRELATIONID, MsmqMessageId.FromString(property.CorrelationId));
                }

                if (null != property.Extension)
                {
                    this.extension = new BufferProperty(this, UnsafeNativeMethods.PROPID_M_EXTENSION, property.Extension);
                }

                if (null != property.Label)
                {
                    this.label = new StringProperty(this, UnsafeNativeMethods.PROPID_M_LABEL, property.Label);
                }

                if (property.Priority.HasValue)
                {
                    this.priority = new ByteProperty(this, UnsafeNativeMethods.PROPID_M_PRIORITY, (byte)property.Priority.Value);
                }

                if (null != property.ResponseQueue)
                {
                    EnsureResponseQueueProperty(property.ResponseQueue);
                }

                if (property.TimeToReachQueue.HasValue)
                {
                    EnsureTimeToReachQueueProperty(MsmqDuration.FromTimeSpan(property.TimeToReachQueue.Value));
                }
            }
        protected override void OnSend(Message message, TimeSpan timeout)
        {
            MessageProperties properties = message.Properties;
            Stream            stream     = null;

            MsmqIntegrationMessageProperty property = MsmqIntegrationMessageProperty.Get(message);

            if (null == property)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(SR.GetString(SR.MsmqMessageDoesntHaveIntegrationProperty)));
            }
            if (null != property.Body)
            {
                stream = this.factory.Serialize(property);
            }

            int size;

            if (stream == null)
            {
                size = 0;
            }
            else
            {
                if (stream.Length > int.MaxValue)
                {
                    throw TraceUtility.ThrowHelperError(new ProtocolException(SR.GetString(SR.MessageSizeMustBeInIntegerRange)), message);
                }

                size = (int)stream.Length;
            }

            using (MsmqIntegrationOutputMessage msmqMessage = new MsmqIntegrationOutputMessage(this.factory, size, this.RemoteAddress, property))
            {
                msmqMessage.ApplyCertificateIfNeeded(this.certificateTokenProvider, this.factory.MsmqTransportSecurity.MsmqAuthenticationMode, timeout);

                if (stream != null)
                {
                    stream.Position = 0;
                    for (int bytesRemaining = size; bytesRemaining > 0;)
                    {
                        int bytesRead = stream.Read(msmqMessage.Body.Buffer, 0, bytesRemaining);
                        bytesRemaining -= bytesRead;
                    }
                }

                bool lockHeld = false;
                try
                {
                    Msmq.EnterXPSendLock(out lockHeld, this.factory.MsmqTransportSecurity.MsmqProtectionLevel);
                    this.msmqQueue.Send(msmqMessage, this.transactionMode);
                    MsmqDiagnostics.DatagramSent(msmqMessage.MessageId, message);
                    property.Id = MsmqMessageId.ToString(msmqMessage.MessageId.Buffer);
                }
                catch (MsmqException ex)
                {
                    if (ex.FaultSender)
                    {
                        this.Fault();
                    }
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ex.Normalized);
                }
                finally
                {
                    if (lockHeld)
                    {
                        Msmq.LeaveXPSendLock();
                    }
                }
            }
        }