コード例 #1
0
ファイル: MtomMessageEncoder.cs プロジェクト: HongGit/wcf
        internal XmlReader TakeStreamedReader(Stream stream, string contentType, bool isMtomContentType)
        {
            if (_streamedReaderPool == null)
            {
                lock (_thisLock)
                {
                    if (_streamedReaderPool == null)
                    {
                        _streamedReaderPool = new SynchronizedPool <XmlDictionaryReader>(MaxReadPoolSize);
                    }
                }
            }
            XmlDictionaryReader xmlReader = _streamedReaderPool.Take();

            try
            {
                if (contentType == null || isMtomContentType)
                {
                    if (xmlReader != null && xmlReader is IXmlMtomReaderInitializer)
                    {
                        ((IXmlMtomReaderInitializer)xmlReader).SetInput(stream, MtomMessageEncoderFactory.GetSupportedEncodings(), contentType, ReaderQuotas, MaxBufferSize, _onStreamedReaderClose);
                    }
                    else
                    {
                        xmlReader = XmlMtomReader.Create(stream, MtomMessageEncoderFactory.GetSupportedEncodings(), contentType, ReaderQuotas, MaxBufferSize, _onStreamedReaderClose);
                        if (WcfEventSource.Instance.ReadPoolMissIsEnabled())
                        {
                            WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name);
                        }
                    }
                }
                else
                {
                    if (xmlReader != null && xmlReader is IXmlTextReaderInitializer)
                    {
                        ((IXmlTextReaderInitializer)xmlReader).SetInput(stream, TextMessageEncoderFactory.GetEncodingFromContentType(contentType, ContentEncodingMap), ReaderQuotas, _onStreamedReaderClose);
                    }
                    else
                    {
                        xmlReader = XmlDictionaryReader.CreateTextReader(stream, TextMessageEncoderFactory.GetEncodingFromContentType(contentType, ContentEncodingMap), ReaderQuotas, _onStreamedReaderClose);
                        if (WcfEventSource.Instance.ReadPoolMissIsEnabled())
                        {
                            WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name);
                        }
                    }
                }
            }
            catch (FormatException fe)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                              SR.SFxErrorCreatingMtomReader, fe));
            }
            catch (XmlException xe)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                              SR.SFxErrorCreatingMtomReader, xe));
            }

            return(xmlReader);
        }
コード例 #2
0
ファイル: MtomMessageEncoder.cs プロジェクト: HongGit/wcf
            protected override XmlDictionaryReader TakeXmlReader()
            {
                try
                {
                    ArraySegment <byte> buffer = Buffer;

                    XmlDictionaryReader xmlReader = _readerPool.Take();
                    if (_contentType == null || _messageEncoder.IsMTOMContentType(_contentType))
                    {
                        if (xmlReader != null && xmlReader is IXmlMtomReaderInitializer)
                        {
                            ((IXmlMtomReaderInitializer)xmlReader).SetInput(buffer.Array, buffer.Offset, buffer.Count, MtomMessageEncoderFactory.GetSupportedEncodings(), _contentType, Quotas, _messageEncoder.MaxBufferSize, _onClose);
                        }
                        else
                        {
                            xmlReader = XmlMtomReader.Create(buffer.Array, buffer.Offset, buffer.Count, MtomMessageEncoderFactory.GetSupportedEncodings(), _contentType, Quotas, _messageEncoder.MaxBufferSize, _onClose);
                            if (WcfEventSource.Instance.ReadPoolMissIsEnabled())
                            {
                                WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name);
                            }
                        }
                    }
                    else
                    {
                        if (xmlReader != null && xmlReader is IXmlTextReaderInitializer)
                        {
                            ((IXmlTextReaderInitializer)xmlReader).SetInput(buffer.Array, buffer.Offset, buffer.Count, TextMessageEncoderFactory.GetEncodingFromContentType(_contentType, _messageEncoder._factory.ContentEncodingMap), Quotas, _onClose);
                        }
                        else
                        {
                            xmlReader = XmlDictionaryReader.CreateTextReader(buffer.Array, buffer.Offset, buffer.Count, TextMessageEncoderFactory.GetEncodingFromContentType(_contentType, _messageEncoder._factory.ContentEncodingMap), Quotas, _onClose);
                            if (WcfEventSource.Instance.ReadPoolMissIsEnabled())
                            {
                                WcfEventSource.Instance.ReadPoolMiss(xmlReader.GetType().Name);
                            }
                        }
                    }
                    return(xmlReader);
                }
                catch (FormatException fe)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                                  SR.SFxErrorCreatingMtomReader, fe));
                }
                catch (XmlException xe)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException(
                                                                                  SR.SFxErrorCreatingMtomReader, xe));
                }
            }
コード例 #3
0
ファイル: MtomMessageEncoder.cs プロジェクト: HongGit/wcf
        private ArraySegment <byte> WriteMessageInternal(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset, string startInfo, string boundary, string startUri, bool writeMessageHeaders)
        {
            if (message == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message));
            }
            if (bufferManager == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(bufferManager));
            }
            if (maxMessageSize < 0)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(maxMessageSize), maxMessageSize,
                                                                                                          SR.ValueMustBeNonNegative));
            }
            if (messageOffset < 0 || messageOffset > maxMessageSize)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(messageOffset), messageOffset,
                                                                                                          SR.Format(SR.ValueMustBeInRange, 0, maxMessageSize)));
            }
            ThrowIfMismatchedMessageVersion(message);

            EventTraceActivity eventTraceActivity = null;

            if (WcfEventSource.Instance.MtomMessageEncodingStartIsEnabled())
            {
                eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message);
                WcfEventSource.Instance.MtomMessageEncodingStart(eventTraceActivity);
            }

            message.Properties.Encoder = this;

            MtomBufferedMessageWriter messageWriter = _factory.TakeBufferedWriter(this);

            messageWriter._startInfo           = startInfo;
            messageWriter._boundary            = boundary;
            messageWriter._startUri            = startUri;
            messageWriter._writeMessageHeaders = writeMessageHeaders;
            messageWriter._maxSizeInBytes      = maxMessageSize;
            ArraySegment <byte> messageData = messageWriter.WriteMessage(message, bufferManager, messageOffset, maxMessageSize);

            _factory.ReturnMessageWriter(messageWriter);

            if (WcfEventSource.Instance.MessageWrittenByEncoderIsEnabled() && messageData != null)
            {
                WcfEventSource.Instance.MessageWrittenByEncoder(
                    eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message),
                    messageData.Count,
                    this);
            }

            if (MessageLogger.LogMessagesAtTransportLevel)
            {
                string contentType = null;
                if (boundary != null)
                {
                    contentType = FormatContentType(boundary, startInfo ?? GenerateStartInfoString());
                }

                XmlDictionaryReader xmlDictionaryReader = XmlMtomReader.Create(messageData.Array, messageData.Offset, messageData.Count, MtomMessageEncoderFactory.GetSupportedEncodings(), contentType, XmlDictionaryReaderQuotas.Max, int.MaxValue, null);
                MessageLogger.LogMessage(ref message, xmlDictionaryReader, MessageLoggingSource.TransportSend);
            }

            return(messageData);
        }