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
- XamlReader.cs
- Propagator.JoinPropagator.cs
- Model3D.cs
- WrapperEqualityComparer.cs
- EditorZoneBase.cs
- SelfSignedCertificate.cs
- FontEmbeddingManager.cs
- Rules.cs
- BezierSegment.cs
- XPathPatternParser.cs
- StrokeCollectionDefaultValueFactory.cs
- SmiRecordBuffer.cs
- StreamInfo.cs
- StructuralCache.cs
- MetadataItem.cs
- Clipboard.cs
- TextSelectionProcessor.cs
- FileUtil.cs
- CompositeActivityTypeDescriptor.cs
- SingleObjectCollection.cs
- CounterCreationDataConverter.cs
- RoleManagerEventArgs.cs
- DbParameterHelper.cs
- SQLBoolean.cs
- ToolStripDropDownButton.cs
- DataGridHeaderBorder.cs
- SystemParameters.cs
- SqlTransaction.cs
- FontInfo.cs
- XmlValueConverter.cs
- UserControlCodeDomTreeGenerator.cs
- DelegateArgumentValue.cs
- DbFunctionCommandTree.cs
- EdmType.cs
- WebPartZone.cs
- ScriptResourceAttribute.cs
- AddInControllerImpl.cs
- DriveNotFoundException.cs
- RefType.cs
- Simplifier.cs
- InputReportEventArgs.cs
- EntityClassGenerator.cs
- DefaultAsyncDataDispatcher.cs
- HtmlInputButton.cs
- ParseHttpDate.cs
- ListParagraph.cs
- HelpProvider.cs
- ConfigurationErrorsException.cs
- EntityDataSourceEntityTypeFilterItem.cs
- IOException.cs
- SwitchCase.cs
- MdImport.cs
- AppendHelper.cs
- FlowDocumentPage.cs
- WinFormsUtils.cs
- ReflectionUtil.cs
- PopupControlService.cs
- SHA1Managed.cs
- ObjectStateManager.cs
- smtpconnection.cs
- DesignTimeTemplateParser.cs
- UserMapPath.cs
- DataServiceStreamProviderWrapper.cs
- RegexCharClass.cs
- Point3DKeyFrameCollection.cs
- KnownBoxes.cs
- WebBrowserSiteBase.cs
- PerformanceCounterPermissionEntry.cs
- ButtonAutomationPeer.cs
- HtmlAnchor.cs
- SizeIndependentAnimationStorage.cs
- GridItem.cs
- SizeF.cs
- ToolStripContentPanelDesigner.cs
- figurelengthconverter.cs
- JournalEntryStack.cs
- RuntimeVariableList.cs
- XmlDocumentFragment.cs
- DataSetMappper.cs
- Compiler.cs
- XmlCharCheckingReader.cs
- UTF7Encoding.cs
- StatusBarDrawItemEvent.cs
- DtrList.cs
- Debugger.cs
- FileNotFoundException.cs
- ElementAction.cs
- VectorAnimationBase.cs
- BaseDataListComponentEditor.cs
- MissingMethodException.cs
- HandledMouseEvent.cs
- XmlTextWriter.cs
- SqlCharStream.cs
- WebPartTracker.cs
- HealthMonitoringSectionHelper.cs
- NonParentingControl.cs
- Source.cs
- AuthenticationService.cs
- NavigationEventArgs.cs
- UnsafeNativeMethodsMilCoreApi.cs