Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Channels / WebMessageEncoderFactory.cs / 1 / WebMessageEncoderFactory.cs
//---------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------- namespace System.ServiceModel.Channels { using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Net.Mime; using System.ServiceModel.Diagnostics; using System.Text; using System.Xml; using System.Net; using System.ServiceModel.Web; class WebMessageEncoderFactory : MessageEncoderFactory { WebMessageEncoder messageEncoder; public WebMessageEncoderFactory(Encoding writeEncoding, int maxReadPoolSize, int maxWritePoolSize, XmlDictionaryReaderQuotas quotas, WebContentTypeMapper contentTypeMapper) { messageEncoder = new WebMessageEncoder(writeEncoding, maxReadPoolSize, maxWritePoolSize, quotas, contentTypeMapper); } public override MessageEncoder Encoder { get { return messageEncoder; } } public override MessageVersion MessageVersion { get { return messageEncoder.MessageVersion; } } class WebMessageEncoder : MessageEncoder { const string defaultMediaType = "application/xml"; WebContentTypeMapper contentTypeMapper; string defaultContentType; MessageEncoder jsonMessageEncoder; int maxReadPoolSize; int maxWritePoolSize; MessageEncoder rawMessageEncoder; XmlDictionaryReaderQuotas readerQuotas; MessageEncoder textMessageEncoder; object thisLock; Encoding writeEncoding; public WebMessageEncoder(Encoding writeEncoding, int maxReadPoolSize, int maxWritePoolSize, XmlDictionaryReaderQuotas quotas, WebContentTypeMapper contentTypeMapper) { if (writeEncoding == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("writeEncoding"); } this.thisLock = new object(); TextEncoderDefaults.ValidateEncoding(writeEncoding); this.writeEncoding = writeEncoding; this.maxReadPoolSize = maxReadPoolSize; this.maxWritePoolSize = maxWritePoolSize; this.contentTypeMapper = contentTypeMapper; this.readerQuotas = new XmlDictionaryReaderQuotas(); quotas.CopyTo(this.readerQuotas); this.defaultContentType = String.Format(CultureInfo.InvariantCulture, "{0}; charset={1}", defaultMediaType, TextEncoderDefaults.EncodingToCharSet(writeEncoding)); } public override string ContentType { get { return this.defaultContentType; } } public override string MediaType { get { return defaultMediaType; } } public override MessageVersion MessageVersion { get { return MessageVersion.None; } } MessageEncoder JsonMessageEncoder { get { if (jsonMessageEncoder == null) { lock (ThisLock) { if (jsonMessageEncoder == null) { jsonMessageEncoder = new JsonMessageEncoderFactory(writeEncoding, maxReadPoolSize, maxWritePoolSize, readerQuotas).Encoder; } } } return jsonMessageEncoder; } } MessageEncoder RawMessageEncoder { get { if (rawMessageEncoder == null) { lock (ThisLock) { if (rawMessageEncoder == null) { rawMessageEncoder = new HttpStreamMessageEncoderFactory(readerQuotas).Encoder; } } } return rawMessageEncoder; } } MessageEncoder TextMessageEncoder { get { if (textMessageEncoder == null) { lock (ThisLock) { if (textMessageEncoder == null) { textMessageEncoder = new TextMessageEncoderFactory(MessageVersion.None, writeEncoding, maxReadPoolSize, maxWritePoolSize, readerQuotas).Encoder; } } } return textMessageEncoder; } } object ThisLock { get { return thisLock; } } public override bool IsContentTypeSupported(string contentType) { if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } WebContentFormat messageFormat; if (TryGetContentTypeMapping(contentType, out messageFormat) && (messageFormat != WebContentFormat.Default)) { return true; } return RawMessageEncoder.IsContentTypeSupported(contentType) || JsonMessageEncoder.IsContentTypeSupported(contentType) || TextMessageEncoder.IsContentTypeSupported(contentType); } public override Message ReadMessage(ArraySegmentbuffer, BufferManager bufferManager, string contentType) { if (bufferManager == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("bufferManager")); } WebContentFormat format = GetFormatForContentType(contentType); Message message; switch (format) { case WebContentFormat.Json: message = JsonMessageEncoder.ReadMessage(buffer, bufferManager, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); break; case WebContentFormat.Xml: message = TextMessageEncoder.ReadMessage(buffer, bufferManager, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); break; case WebContentFormat.Raw: message = RawMessageEncoder.ReadMessage(buffer, bufferManager, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); break; default: Fx.Assert("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw"); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } return message; } public override Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType) { if (stream == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("stream")); } WebContentFormat format = GetFormatForContentType(contentType); Message message; switch (format) { case WebContentFormat.Json: message = JsonMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); break; case WebContentFormat.Xml: message = TextMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); break; case WebContentFormat.Raw: message = RawMessageEncoder.ReadMessage(stream, maxSizeOfHeaders, contentType); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); break; default: Fx.Assert("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw"); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } return message; } public override ArraySegment WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (bufferManager == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("bufferManager"), message); } if (maxMessageSize < 0) { throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("maxMessageSize", maxMessageSize, SR2.GetString(SR2.ValueMustBeNonNegative)), message); } if (messageOffset < 0 || messageOffset > maxMessageSize) { throw TraceUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageOffset", messageOffset, SR2.GetString(SR2.JsonValueMustBeInRange, 0, maxMessageSize)), message); } ThrowIfMismatchedMessageVersion(message); WebContentFormat messageFormat = ExtractFormatFromMessage(message); switch (messageFormat) { case WebContentFormat.Json: return JsonMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset); case WebContentFormat.Xml: return TextMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset); case WebContentFormat.Raw: return RawMessageEncoder.WriteMessage(message, maxMessageSize, bufferManager, messageOffset); default: Fx.Assert("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw"); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } } public override void WriteMessage(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } ThrowIfMismatchedMessageVersion(message); WebContentFormat messageFormat = ExtractFormatFromMessage(message); switch (messageFormat) { case WebContentFormat.Json: JsonMessageEncoder.WriteMessage(message, stream); break; case WebContentFormat.Xml: TextMessageEncoder.WriteMessage(message, stream); break; case WebContentFormat.Raw: RawMessageEncoder.WriteMessage(message, stream); break; default: Fx.Assert("This should never get hit because GetFormatForContentType shouldn't return a WebContentFormat other than Json, Xml, and Raw"); throw DiagnosticUtility.ExceptionUtility.ThrowHelperInternal(false); } } internal override bool IsCharSetSupported(string charSet) { Encoding tmp; return TextEncoderDefaults.TryGetEncoding(charSet, out tmp); } WebContentFormat ExtractFormatFromMessage(Message message) { object messageFormatProperty; message.Properties.TryGetValue(WebBodyFormatMessageProperty.Name, out messageFormatProperty); if (messageFormatProperty == null) { return WebContentFormat.Xml; } WebBodyFormatMessageProperty typedMessageFormatProperty = messageFormatProperty as WebBodyFormatMessageProperty; if ((typedMessageFormatProperty == null) || (typedMessageFormatProperty.Format == WebContentFormat.Default)) { return WebContentFormat.Xml; } return typedMessageFormatProperty.Format; } WebContentFormat GetFormatForContentType(string contentType) { WebContentFormat messageFormat; if (TryGetContentTypeMapping(contentType, out messageFormat) && (messageFormat != WebContentFormat.Default)) { return messageFormat; } // Don't pass on null content types to IsContentTypeSupported methods -- they might throw. // If null content type isn't already mapped, return the default format of Raw. if (contentType == null) { return WebContentFormat.Raw; } if (JsonMessageEncoder.IsContentTypeSupported(contentType)) { return WebContentFormat.Json; } if (TextMessageEncoder.IsContentTypeSupported(contentType)) { return WebContentFormat.Xml; } return WebContentFormat.Raw; } bool TryGetContentTypeMapping(string contentType, out WebContentFormat format) { if (contentTypeMapper == null) { format = WebContentFormat.Default; return false; } try { format = contentTypeMapper.GetMessageFormatForContentType(contentType); if (!WebContentFormatHelper.IsDefined(format)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR2.GetString(SR2.UnknownWebEncodingFormat, contentType, format))); } return true; } catch (Exception e) { if (DiagnosticUtility.IsFatal(e)) { throw; } throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CommunicationException( SR2.GetString(SR2.ErrorEncounteredInContentTypeMapper), e)); } } } } } // 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
- Group.cs
- ClientSettings.cs
- WindowsListBox.cs
- PackWebRequestFactory.cs
- SafeRightsManagementSessionHandle.cs
- WpfGeneratedKnownTypes.cs
- QilBinary.cs
- ObjectDataSourceStatusEventArgs.cs
- Atom10FormatterFactory.cs
- MetadataSection.cs
- TargetConverter.cs
- UInt16Converter.cs
- TraceSwitch.cs
- IdentityValidationException.cs
- ButtonFieldBase.cs
- QEncodedStream.cs
- WasAdminWrapper.cs
- XmlIlGenerator.cs
- CookielessData.cs
- IncrementalCompileAnalyzer.cs
- DependencyObject.cs
- PropertyItemInternal.cs
- RsaSecurityTokenParameters.cs
- MenuItemBindingCollection.cs
- TextServicesContext.cs
- CaseCqlBlock.cs
- IgnoreFlushAndCloseStream.cs
- LocatorPartList.cs
- InputLanguageProfileNotifySink.cs
- XmlSchemaAnyAttribute.cs
- JsonReaderWriterFactory.cs
- Control.cs
- PropertyMap.cs
- DateTimeFormatInfo.cs
- WS2007HttpBindingElement.cs
- OrCondition.cs
- TitleStyle.cs
- TokenizerHelper.cs
- Configuration.cs
- AccessibleObject.cs
- MulticastIPAddressInformationCollection.cs
- PathTooLongException.cs
- ModelPerspective.cs
- ConnectionPoolManager.cs
- CollectionView.cs
- PolygonHotSpot.cs
- GridViewPageEventArgs.cs
- PropertyInformation.cs
- TrackingDataItem.cs
- UnsafeNativeMethods.cs
- Interlocked.cs
- XmlNodeChangedEventManager.cs
- SR.cs
- TreeWalkHelper.cs
- Transform3DGroup.cs
- ClientSettings.cs
- KeyEventArgs.cs
- WsatServiceAddress.cs
- SchemaTypeEmitter.cs
- DataGridPreparingCellForEditEventArgs.cs
- State.cs
- ManagementDateTime.cs
- XmlMembersMapping.cs
- AppSettingsSection.cs
- XmlSchemaComplexContent.cs
- InfocardExtendedInformationCollection.cs
- TraceUtils.cs
- CmsInterop.cs
- DispatcherBuilder.cs
- EntityCommandDefinition.cs
- MergablePropertyAttribute.cs
- PropertyToken.cs
- ConnectionManagementSection.cs
- TextServicesHost.cs
- SymLanguageVendor.cs
- FontStyleConverter.cs
- EncoderBestFitFallback.cs
- Pkcs7Signer.cs
- returneventsaver.cs
- CombinedGeometry.cs
- MatrixIndependentAnimationStorage.cs
- ConstraintCollection.cs
- MinimizableAttributeTypeConverter.cs
- SourceInterpreter.cs
- Italic.cs
- XamlToRtfParser.cs
- SerializationEventsCache.cs
- WsdlHelpGeneratorElement.cs
- DispatchOperation.cs
- IdnMapping.cs
- ToolStripPanel.cs
- _HelperAsyncResults.cs
- StringInfo.cs
- ProxyGenerationError.cs
- Rfc2898DeriveBytes.cs
- GlyphingCache.cs
- PreviewKeyDownEventArgs.cs
- ElementHostPropertyMap.cs
- EndEvent.cs
- DrawingAttributesDefaultValueFactory.cs