Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Web / WebOperationContext.cs / 1305376 / WebOperationContext.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- #pragma warning disable 1634, 1691 namespace System.ServiceModel.Web { using System; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Net; using System.Runtime.Serialization.Json; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Syndication; using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; using System.ServiceModel.Dispatcher; public class WebOperationContext : IExtension{ internal static readonly string DefaultTextMediaType = "text/plain"; internal static readonly string DefaultJsonMediaType = JsonGlobals.applicationJsonMediaType; internal static readonly string DefaultXmlMediaType = "application/xml"; internal static readonly string DefaultAtomMediaType = "application/atom+xml"; internal static readonly string DefaultStreamMediaType = WebHttpBehavior.defaultStreamContentType; OperationContext operationContext; public WebOperationContext(OperationContext operationContext) { if (operationContext == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("operationContext"); } this.operationContext = operationContext; #pragma warning disable 56506 // [....], operationContext.Extensions is never null if (operationContext.Extensions.Find () == null) { operationContext.Extensions.Add(this); } #pragma warning enable 56506 } public static WebOperationContext Current { get { if (OperationContext.Current == null) { return null; } WebOperationContext existing = OperationContext.Current.Extensions.Find (); if (existing != null) { return existing; } return new WebOperationContext(OperationContext.Current); } } public IncomingWebRequestContext IncomingRequest { get { return new IncomingWebRequestContext(this.operationContext); } } public IncomingWebResponseContext IncomingResponse { get { return new IncomingWebResponseContext(this.operationContext); } } public OutgoingWebRequestContext OutgoingRequest { get { return new OutgoingWebRequestContext(this.operationContext); } } public OutgoingWebResponseContext OutgoingResponse { get { return new OutgoingWebResponseContext(this.operationContext); } } public void Attach(OperationContext owner) { } public void Detach(OperationContext owner) { } public Message CreateJsonResponse (T instance) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); return CreateJsonResponse (instance, serializer); } [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "CreateJsonMessage requires the DataContractJsonSerializer. Allowing the base type XmlObjectSerializer would let deveopers supply a non-Json Serializer.")] public Message CreateJsonResponse (T instance, DataContractJsonSerializer serializer) { if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, instance, serializer); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.JsonProperty); AddContentType(WebOperationContext.DefaultJsonMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateXmlResponse (T instance) { System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(T)); return CreateXmlResponse(instance, serializer); } public Message CreateXmlResponse (T instance, System.Runtime.Serialization.XmlObjectSerializer serializer) { if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, instance, serializer); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultXmlMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateXmlResponse (T instance, XmlSerializer serializer) { if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, new XmlSerializerBodyWriter(instance, serializer)); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultXmlMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Other XNode derived types such as XAttribute don't make sense in this context, so we are not using the XNode base type.")] public Message CreateXmlResponse(XDocument document) { if (document == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document"); } Message message; if (document.FirstNode == null) { message = Message.CreateMessage(MessageVersion.None, (string)null); } else { message = Message.CreateMessage(MessageVersion.None, (string)null, document.CreateReader()); } message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultXmlMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Other XNode derived types such as XAttribute don't make sense in this context, so we are not using the XNode base type.")] public Message CreateXmlResponse(XElement element) { if (element == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("element"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, element.CreateReader()); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultXmlMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateAtom10Response(SyndicationItem item) { if (item == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("item"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, item.GetAtom10Formatter()); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultAtomMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateAtom10Response(SyndicationFeed feed) { if (feed == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("feed"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, feed.GetAtom10Formatter()); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultAtomMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateAtom10Response(ServiceDocument document) { if (document == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("document"); } Message message = Message.CreateMessage(MessageVersion.None, (string)null, document.GetFormatter()); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.XmlProperty); AddContentType(WebOperationContext.DefaultAtomMediaType, this.OutgoingResponse.BindingWriteEncoding); return message; } public Message CreateTextResponse(string text) { return CreateTextResponse(text, WebOperationContext.DefaultTextMediaType, Encoding.UTF8); } public Message CreateTextResponse(string text, string contentType) { return CreateTextResponse(text, contentType, Encoding.UTF8); } public Message CreateTextResponse(string text, string contentType, Encoding encoding) { if (text == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("text"); } if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } if (encoding == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding"); } Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter((stream) => { byte[] preamble = encoding.GetPreamble(); if (preamble.Length > 0) { stream.Write(preamble, 0, preamble.Length); } byte[] bytes = encoding.GetBytes(text); stream.Write(bytes, 0, bytes.Length); stream.Flush(); })); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); AddContentType(contentType, null); return message; } public Message CreateTextResponse(Action textWriter, string contentType) { Encoding encoding = this.OutgoingResponse.BindingWriteEncoding; if (encoding == null) { encoding = Encoding.UTF8; } return CreateTextResponse(textWriter, contentType, encoding); } public Message CreateTextResponse(Action textWriter, string contentType, Encoding encoding) { if (textWriter == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("textWriter"); } if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } if (encoding == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("encoding"); } Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter((stream) => { using (TextWriter writer = new StreamWriter(stream, encoding)) { textWriter(writer); } })); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); AddContentType(contentType, null); return message; } public Message CreateStreamResponse(Stream stream, string contentType) { if (stream == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("stream"); } if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } Message message = new HttpStreamMessage(stream); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); AddContentType(contentType, null); return message; } [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Using the StreamBodyWriter type instead of the BodyWriter type for discoverability. The StreamBodyWriter provides a helpful overload of the OnWriteBodyContents method that takes a Stream")] public Message CreateStreamResponse(StreamBodyWriter bodyWriter, string contentType) { if (bodyWriter == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("bodyWriter"); } if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } Message message = new HttpStreamMessage(bodyWriter); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); AddContentType(contentType, null); return message; } public Message CreateStreamResponse(Action streamWriter, string contentType) { if (streamWriter == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("streamWriter"); } if (contentType == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("contentType"); } Message message = new HttpStreamMessage(StreamBodyWriter.CreateStreamBodyWriter(streamWriter)); message.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); AddContentType(contentType, null); return message; } public UriTemplate GetUriTemplate(string operationName) { if (String.IsNullOrEmpty(operationName)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("operationName"); } WebHttpDispatchOperationSelector selector = OperationContext.Current.EndpointDispatcher.DispatchRuntime.OperationSelector as WebHttpDispatchOperationSelector; if (selector == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR2.GetString(SR2.OperationSelectorNotWebSelector, typeof(WebHttpDispatchOperationSelector)))); } return selector.GetUriTemplate(operationName); } void AddContentType(string contentType, Encoding encoding) { if (string.IsNullOrEmpty(this.OutgoingResponse.ContentType)) { if (encoding != null) { contentType = WebMessageEncoderFactory.GetContentType(contentType, encoding); } this.OutgoingResponse.ContentType = contentType; } } class XmlSerializerBodyWriter : BodyWriter { object instance; XmlSerializer serializer; public XmlSerializerBodyWriter(object instance, XmlSerializer serializer) : base(false) { if (instance == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("instance"); } if (serializer == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("serializer"); } this.instance = instance; this.serializer = serializer; } protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { serializer.Serialize(writer, instance); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- PrincipalPermission.cs
- XmlUrlResolver.cs
- BindingGroup.cs
- AppDomainUnloadedException.cs
- ToolStripItemBehavior.cs
- ColorConvertedBitmapExtension.cs
- MethodBody.cs
- IncrementalHitTester.cs
- SimpleBitVector32.cs
- CustomCategoryAttribute.cs
- BindStream.cs
- XmlDataSourceView.cs
- FormViewRow.cs
- BufferedResponseStream.cs
- DataGridViewCellCancelEventArgs.cs
- DataGridViewCellMouseEventArgs.cs
- Timer.cs
- Base64Decoder.cs
- basemetadatamappingvisitor.cs
- DiscoveryClient.cs
- ToolstripProfessionalRenderer.cs
- DataFormat.cs
- XmlWhitespace.cs
- MimeBasePart.cs
- SmtpNtlmAuthenticationModule.cs
- Soap.cs
- PartialTrustVisibleAssembliesSection.cs
- PartialClassGenerationTask.cs
- Listen.cs
- PropertyPathWorker.cs
- WebPartVerbsEventArgs.cs
- ListenerSessionConnection.cs
- SQLBinaryStorage.cs
- FormCollection.cs
- BindToObject.cs
- SqlConnection.cs
- PageCodeDomTreeGenerator.cs
- Timer.cs
- OptimizerPatterns.cs
- _IPv4Address.cs
- SafeCryptoHandles.cs
- Msmq4SubqueuePoisonHandler.cs
- DefaultCommandExtensionCallback.cs
- CompoundFileStorageReference.cs
- ToolZone.cs
- Int32Rect.cs
- RequestQueryProcessor.cs
- UserInitiatedNavigationPermission.cs
- Action.cs
- DataSourceCache.cs
- FileClassifier.cs
- EndOfStreamException.cs
- RegexMatch.cs
- WebBrowserDesigner.cs
- WindowsBrush.cs
- ComponentDispatcher.cs
- DesignerExtenders.cs
- Policy.cs
- DrawItemEvent.cs
- SystemException.cs
- SecurityKeyType.cs
- Cursor.cs
- SoapDocumentMethodAttribute.cs
- ControlAdapter.cs
- HostedElements.cs
- GridViewDeletedEventArgs.cs
- DragDeltaEventArgs.cs
- OptimisticConcurrencyException.cs
- httpserverutility.cs
- ModelService.cs
- ParseHttpDate.cs
- CacheManager.cs
- ErrorStyle.cs
- ToolStripPanel.cs
- PagePropertiesChangingEventArgs.cs
- WindowsRegion.cs
- XmlSchema.cs
- DefaultValueConverter.cs
- ControlPaint.cs
- DataGridViewRowHeightInfoPushedEventArgs.cs
- MobileControlBuilder.cs
- DesignRelation.cs
- StructuralComparisons.cs
- AdapterDictionary.cs
- BitmapEffectCollection.cs
- WebBrowser.cs
- Focus.cs
- WebExceptionStatus.cs
- PersistChildrenAttribute.cs
- OdbcException.cs
- AsymmetricKeyExchangeDeformatter.cs
- ListItemParagraph.cs
- Help.cs
- SqlTriggerAttribute.cs
- Control.cs
- ParameterBuilder.cs
- WindowsGraphicsCacheManager.cs
- TypePropertyEditor.cs
- XmlSchemaAttributeGroupRef.cs
- BitArray.cs