Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / MsmqDecodeHelper.cs / 1 / MsmqDecodeHelper.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.ServiceModel.MsmqIntegration; using System.ServiceModel.Security; using System.Transactions; using System.Xml; using System.Xml.Serialization; static class MsmqDecodeHelper { static ActiveXSerializer activeXSerializer; static BinaryFormatter binaryFormatter; const int defaultMaxViaSize = 2048; const int defaultMaxContentTypeSize = 256; static ActiveXSerializer ActiveXSerializer { get { if (null == activeXSerializer) activeXSerializer = new ActiveXSerializer(); return activeXSerializer; } } static BinaryFormatter BinaryFormatter { get { if (null == binaryFormatter) binaryFormatter = new BinaryFormatter(); return binaryFormatter; } } static void ReadServerMode(MsmqChannelListenerBase listener, ServerModeDecoder modeDecoder, byte[] incoming, long lookupId, ref int offset, ref int size) { for (;;) { if (size <= 0) { throw listener.NormalizePoisonException(lookupId, modeDecoder.CreatePrematureEOFException()); } int decoded = modeDecoder.Decode(incoming, offset, size); offset += decoded; size -= decoded; if (ServerModeDecoder.State.Done == modeDecoder.CurrentState) break; } } internal static Message DecodeTransportDatagram(MsmqInputChannelListener listener, MsmqReceiveHelper receiver, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty) { using (MsmqDiagnostics.BoundReceiveBytesOperation()) { long lookupId = msmqMessage.LookupId.Value; int size = msmqMessage.BodyLength.Value; int offset = 0; byte[] incoming = msmqMessage.Body.Buffer; ServerModeDecoder modeDecoder = new ServerModeDecoder(); try { ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size); } catch (ProtocolException ex) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } if (modeDecoder.Mode != FramingMode.SingletonSized) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame))); } ServerSingletonSizedDecoder decoder = new ServerSingletonSizedDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize); try { for (; ; ) { if (size <= 0) { throw listener.NormalizePoisonException(messageProperty.LookupId, decoder.CreatePrematureEOFException()); } int decoded = decoder.Decode(incoming, offset, size); offset += decoded; size -= decoded; if (decoder.CurrentState == ServerSingletonSizedDecoder.State.Start) break; } } catch (ProtocolException ex) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } if (size > listener.MaxReceivedMessageSize) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize)); } if (! listener.MessageEncoderFactory.Encoder.IsContentTypeSupported(decoder.ContentType)) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType))); } byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size); Buffer.BlockCopy(incoming, offset, envelopeBuffer, 0, size); Message message = null; using (MsmqDiagnostics.BoundDecodeOperation()) { try { message = listener.MessageEncoderFactory.Encoder.ReadMessage( new ArraySegment(envelopeBuffer, 0, size), listener.BufferManager); } catch (XmlException e) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e)); } bool closeMessage = true; try { SecurityMessageProperty securityProperty = listener.ValidateSecurity(msmqMessage); if (null != securityProperty) message.Properties.Security = securityProperty; closeMessage = false; MsmqDiagnostics.TransferFromTransport(message); return message; } catch (Exception ex) { if (DiagnosticUtility.IsFatal(ex)) throw; receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } finally { if (closeMessage) { message.Close(); } } } } } internal static IInputSessionChannel DecodeTransportSessiongram ( MsmqInputSessionChannelListener listener, MsmqInputMessage msmqMessage, MsmqMessageProperty messageProperty) { using (MsmqDiagnostics.BoundReceiveBytesOperation()) { long lookupId = msmqMessage.LookupId.Value; int size = msmqMessage.BodyLength.Value; int offset = 0; byte[] incoming = msmqMessage.Body.Buffer; MsmqReceiveHelper receiver = listener.MsmqReceiveHelper; ServerModeDecoder modeDecoder = new ServerModeDecoder(); try { ReadServerMode(listener, modeDecoder, incoming, messageProperty.LookupId, ref offset, ref size); } catch (ProtocolException ex) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } if (modeDecoder.Mode != FramingMode.Simplex) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadFrame))); } MsmqInputSessionChannel channel = null; ServerSessionDecoder sessionDecoder = new ServerSessionDecoder(0, defaultMaxViaSize, defaultMaxContentTypeSize); try { for (; ; ) { if (size <= 0) { throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException()); } int decoded = sessionDecoder.Decode(incoming, offset, size); offset += decoded; size -= decoded; if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState) break; } } catch (ProtocolException ex) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } MessageEncoder encoder = listener.MessageEncoderFactory.CreateSessionEncoder(); if (!encoder.IsContentTypeSupported(sessionDecoder.ContentType)) { receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadContentType))); } channel = new MsmqInputSessionChannel(listener, Transaction.Current); Message message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize); SecurityMessageProperty securityProperty = null; try { securityProperty = listener.ValidateSecurity(msmqMessage); } catch (Exception ex) { if (DiagnosticUtility.IsFatal(ex)) throw; channel.FaultChannel(); receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } if (null != securityProperty) message.Properties.Security = securityProperty; message.Properties[MsmqMessageProperty.Name] = messageProperty; channel.EnqueueAndDispatch(message); listener.RaiseMessageReceived(); for (; ; ) { int decoded; try { if (size <= 0) { channel.FaultChannel(); receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, sessionDecoder.CreatePrematureEOFException()); } decoded = sessionDecoder.Decode(incoming, offset, size); } catch (ProtocolException ex) { channel.FaultChannel(); receiver.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, ex); } offset += decoded; size -= decoded; if (ServerSessionDecoder.State.End == sessionDecoder.CurrentState) break; if (ServerSessionDecoder.State.EnvelopeStart == sessionDecoder.CurrentState) { message = DecodeSessiongramMessage(listener, channel, encoder, messageProperty, incoming, offset, sessionDecoder.EnvelopeSize); if (null != securityProperty) { message.Properties.Security = (SecurityMessageProperty)securityProperty.CreateCopy(); } message.Properties[MsmqMessageProperty.Name] = messageProperty; channel.EnqueueAndDispatch(message); listener.RaiseMessageReceived(); } } channel.Shutdown(); MsmqDiagnostics.SessiongramReceived(channel.Session.Id, msmqMessage.MessageId, channel.InternalPendingItems); return channel; } } static Message DecodeSessiongramMessage( MsmqInputSessionChannelListener listener, MsmqInputSessionChannel channel, MessageEncoder encoder, MsmqMessageProperty messageProperty, byte[] buffer, int offset, int size) { if (size > listener.MaxReceivedMessageSize) { channel.FaultChannel(); listener.MsmqReceiveHelper.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(listener.MaxReceivedMessageSize)); } byte[] envelopeBuffer = listener.BufferManager.TakeBuffer(size); Buffer.BlockCopy(buffer, offset, envelopeBuffer, 0, size); try { Message message = null; using (MsmqDiagnostics.BoundDecodeOperation()) { message = encoder.ReadMessage(new ArraySegment (envelopeBuffer, 0, size), listener.BufferManager); MsmqDiagnostics.TransferFromTransport(message); } return message; } catch (XmlException e) { channel.FaultChannel(); listener.MsmqReceiveHelper.FinalDisposition(messageProperty); throw listener.NormalizePoisonException(messageProperty.LookupId, new ProtocolException(SR.GetString(SR.MsmqBadXml), e)); } } 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(); } } } 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; default: throw new SerializationException(SR.GetString(SR.MsmqUnsupportedSerializationFormat, serializationFormat)); } } static object XmlDeserializeForIntegration(MsmqIntegrationChannelListener listener, Stream stream, long lookupId) { XmlTextReader reader = new XmlTextReader(stream); reader.WhitespaceHandling = WhitespaceHandling.Significant; reader.ProhibitDtd = true; try { foreach (XmlSerializer serializer in listener.XmlSerializerList) { if (serializer.CanDeserialize(reader)) return serializer.Deserialize(reader); } } catch(InvalidOperationException e) { // XmlSerializer throws InvalidOperationException on failure of Deserialize. // We map it to SerializationException to provide consistent interface throw new SerializationException(e.Message); } throw new SerializationException(SR.GetString(SR.MsmqCannotDeserializeXmlMessage)); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebCategoryAttribute.cs
- RelationshipWrapper.cs
- VisualBasicSettingsConverter.cs
- SkinBuilder.cs
- XmlLanguage.cs
- ImageMap.cs
- WebPartManagerDesigner.cs
- HtmlInputText.cs
- FontStyle.cs
- AssemblyNameUtility.cs
- XamlTreeBuilderBamlRecordWriter.cs
- EFDataModelProvider.cs
- QueryOutputWriter.cs
- AllMembershipCondition.cs
- ReceiveMessageRecord.cs
- ThreadStaticAttribute.cs
- Variant.cs
- BamlResourceContent.cs
- DocumentSchemaValidator.cs
- ResourcesChangeInfo.cs
- XamlStackWriter.cs
- PowerStatus.cs
- TemplateNameScope.cs
- UserControlParser.cs
- InputEventArgs.cs
- SystemWebSectionGroup.cs
- XmlDesignerDataSourceView.cs
- XmlSchemaAnyAttribute.cs
- DataTableCollection.cs
- ComplexTypeEmitter.cs
- HexParser.cs
- OutputCacheProfile.cs
- WebPartCollection.cs
- CookieParameter.cs
- SmtpTransport.cs
- VisualStyleInformation.cs
- RSAOAEPKeyExchangeFormatter.cs
- TdsParserStateObject.cs
- IdentityNotMappedException.cs
- SqlClientPermission.cs
- DESCryptoServiceProvider.cs
- ImageCodecInfoPrivate.cs
- HttpApplication.cs
- FormViewInsertedEventArgs.cs
- TableRowCollection.cs
- TraceHandler.cs
- DoubleStorage.cs
- NonSerializedAttribute.cs
- WebPartDisplayModeEventArgs.cs
- SqlDataSourceView.cs
- Typeface.cs
- EndPoint.cs
- SymbolDocumentInfo.cs
- DbConnectionPoolGroupProviderInfo.cs
- XmlTextWriter.cs
- TableParaClient.cs
- CountAggregationOperator.cs
- System.Data.OracleClient_BID.cs
- Visual3D.cs
- DeviceSpecificDesigner.cs
- SByteConverter.cs
- SerializationSectionGroup.cs
- CollectionViewSource.cs
- DataGridCellInfo.cs
- IsolatedStorage.cs
- SizeChangedInfo.cs
- Optimizer.cs
- XmlRawWriterWrapper.cs
- TextRange.cs
- SR.cs
- CellRelation.cs
- EncodingNLS.cs
- Pair.cs
- EndpointBehaviorElementCollection.cs
- WorkflowEnvironment.cs
- PlatformCulture.cs
- ActivityExecutorSurrogate.cs
- PasswordPropertyTextAttribute.cs
- NamespaceEmitter.cs
- BookmarkTable.cs
- MsdtcWrapper.cs
- SecurityMessageProperty.cs
- hebrewshape.cs
- PageAction.cs
- Padding.cs
- DataServiceBuildProvider.cs
- TitleStyle.cs
- DesignTimeSiteMapProvider.cs
- TargetInvocationException.cs
- XmlAnyElementAttribute.cs
- Point.cs
- Set.cs
- BitmapFrame.cs
- XhtmlBasicTextViewAdapter.cs
- LinkLabel.cs
- AuthenticationModuleElement.cs
- QueryCoreOp.cs
- RoutedEventConverter.cs
- WindowsIPAddress.cs
- Page.cs