Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DataWeb / Server / System / Data / Services / ResponseBodyWriter.cs / 1 / ResponseBodyWriter.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // //// Provides a base class for DataWeb services. // // // @owner [....] //--------------------------------------------------------------------- namespace System.Data.Services { using System; using System.Collections; using System.Collections.Generic; using System.Data.Objects; using System.Diagnostics; using System.IO; using System.ServiceModel; using System.ServiceModel.Channels; using System.Text; using System.Xml; using System.Data.Services.Providers; using System.Data.Services.Serializers; ////// Use this class to encapsulate writing the body of the outgoing response /// for a data request. /// internal class ResponseBodyWriter { ///Encoding, if available. private readonly Encoding encoding; ///Whether private readonly bool hasMoved; ///has already moved. Host for the request being processed. private readonly IDataService service; ///Enumerator for results. private readonly IEnumerator queryResults; ///Description of request made to the system. private readonly RequestDescription requestDescription; ///Content format for response. private readonly ContentFormat responseFormat; ///Initializes a new /// Encoding, if available. /// Whetherthat can write the body of a response. has already moved. /// Service for the request being processed. /// Enumerator for results. /// Description of request made to the system. /// Content format for response. internal ResponseBodyWriter( Encoding encoding, bool hasMoved, IDataService service, IEnumerator queryResults, RequestDescription requestDescription, ContentFormat responseFormat) { Debug.Assert(responseFormat != ContentFormat.Unknown, "responseFormat != ContentFormat.Unknown"); this.encoding = encoding; this.hasMoved = hasMoved; this.service = service; this.queryResults = queryResults; this.requestDescription = requestDescription; this.responseFormat = responseFormat; } /// Gets the absolute URI to the service. internal Uri AbsoluteServiceUri { get { return this.service.RequestParams.AbsoluteServiceUri; } } ///Gets the internal IDataServiceHost Host { get { return this.service.Host; } } ///for this response. Gets the internal IDataServiceProvider Provider { get { return this.service.Provider; } } ///for this response. Writes the request body to the specified /// Stream to write to. internal void Write(Stream stream) { Debug.Assert(stream != null, "stream != null"); IExceptionWriter exceptionWriter = null; try { switch (this.responseFormat) { case ContentFormat.Binary: Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue || #endif this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue, this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue"); BinarySerializer binarySerializer = new BinarySerializer(stream); exceptionWriter = binarySerializer; binarySerializer.WriteRequest(this.queryResults.Current); break; case ContentFormat.Text: Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue || #endif this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue, this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue"); TextSerializer textSerializer = new TextSerializer(stream, this.encoding); exceptionWriter = textSerializer; textSerializer.WriteRequest(this.queryResults.Current); break; case ContentFormat.Atom: case ContentFormat.Json: case ContentFormat.PlainXml: Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue, "this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue"); #if ASTORIA_OPEN_OBJECT Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue, "this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue"); #endif Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.Metadata, "this.requestDescription.TargetKind != RequestTargetKind.Metadata"); if (this.requestDescription.TargetKind == RequestTargetKind.ServiceDirectory) { if (this.responseFormat == ContentFormat.Json) { JsonServiceDocumentSerializer serviceSerializer = new JsonServiceDocumentSerializer(stream, this.Provider, this.encoding); exceptionWriter = serviceSerializer; serviceSerializer.WriteRequest(); break; } else { AtomServiceDocumentSerializer serviceSerializer = new AtomServiceDocumentSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding); exceptionWriter = serviceSerializer; serviceSerializer.WriteRequest(); } } else { Serializer serializer; if (ContentFormat.Json == this.responseFormat) { serializer = new JsonSerializer(this.requestDescription, stream, this.AbsoluteServiceUri, this.service, this.encoding, this.Host.ResponseETag); } else if (ContentFormat.PlainXml == this.responseFormat) { serializer = new PlainXmlSerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, stream, this.encoding); } else { Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenProperty || #endif this.requestDescription.TargetKind == RequestTargetKind.Resource, "TargetKind " + this.requestDescription.TargetKind + " == Resource || OpenProperty -- POX should have handled it otherwise."); serializer = new SyndicationSerializer( this.requestDescription, this.AbsoluteServiceUri, this.service, stream, this.encoding, this.Host.ResponseETag, new Atom10FormatterFactory()); } exceptionWriter = serializer; Debug.Assert(exceptionWriter != null, "this.exceptionWriter != null"); serializer.WriteRequest(this.queryResults, this.hasMoved); } break; default: Debug.Assert( this.responseFormat == ContentFormat.MetadataDocument, "responseFormat(" + this.responseFormat + ") == ContentFormat.MetadataDocument -- otherwise exception should have been thrown before"); Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.Metadata, "this.requestDescription.TargetKind == RequestTargetKind.Metadata"); MetadataSerializer metadataSerializer = new MetadataSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding); exceptionWriter = metadataSerializer; metadataSerializer.WriteRequest(); break; } } catch (Exception exception) { if (!WebUtil.IsCatchableExceptionType(exception)) { throw; } // Only JSON and XML are supported. string contentType = (this.responseFormat == ContentFormat.Json) ? XmlConstants.MimeApplicationJson : XmlConstants.MimeApplicationXml; ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter); } finally { WebUtil.Dispose(this.queryResults); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- //. // Copyright (c) Microsoft Corporation. All rights reserved. // //// Provides a base class for DataWeb services. // // // @owner [....] //--------------------------------------------------------------------- namespace System.Data.Services { using System; using System.Collections; using System.Collections.Generic; using System.Data.Objects; using System.Diagnostics; using System.IO; using System.ServiceModel; using System.ServiceModel.Channels; using System.Text; using System.Xml; using System.Data.Services.Providers; using System.Data.Services.Serializers; ////// Use this class to encapsulate writing the body of the outgoing response /// for a data request. /// internal class ResponseBodyWriter { ///Encoding, if available. private readonly Encoding encoding; ///Whether private readonly bool hasMoved; ///has already moved. Host for the request being processed. private readonly IDataService service; ///Enumerator for results. private readonly IEnumerator queryResults; ///Description of request made to the system. private readonly RequestDescription requestDescription; ///Content format for response. private readonly ContentFormat responseFormat; ///Initializes a new /// Encoding, if available. /// Whetherthat can write the body of a response. has already moved. /// Service for the request being processed. /// Enumerator for results. /// Description of request made to the system. /// Content format for response. internal ResponseBodyWriter( Encoding encoding, bool hasMoved, IDataService service, IEnumerator queryResults, RequestDescription requestDescription, ContentFormat responseFormat) { Debug.Assert(responseFormat != ContentFormat.Unknown, "responseFormat != ContentFormat.Unknown"); this.encoding = encoding; this.hasMoved = hasMoved; this.service = service; this.queryResults = queryResults; this.requestDescription = requestDescription; this.responseFormat = responseFormat; } /// Gets the absolute URI to the service. internal Uri AbsoluteServiceUri { get { return this.service.RequestParams.AbsoluteServiceUri; } } ///Gets the internal IDataServiceHost Host { get { return this.service.Host; } } ///for this response. Gets the internal IDataServiceProvider Provider { get { return this.service.Provider; } } ///for this response. Writes the request body to the specified /// Stream to write to. internal void Write(Stream stream) { Debug.Assert(stream != null, "stream != null"); IExceptionWriter exceptionWriter = null; try { switch (this.responseFormat) { case ContentFormat.Binary: Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue || #endif this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue, this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue"); BinarySerializer binarySerializer = new BinarySerializer(stream); exceptionWriter = binarySerializer; binarySerializer.WriteRequest(this.queryResults.Current); break; case ContentFormat.Text: Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue || #endif this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue, this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue"); TextSerializer textSerializer = new TextSerializer(stream, this.encoding); exceptionWriter = textSerializer; textSerializer.WriteRequest(this.queryResults.Current); break; case ContentFormat.Atom: case ContentFormat.Json: case ContentFormat.PlainXml: Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue, "this.requestDescription.TargetKind != RequestTargetKind.PrimitiveValue"); #if ASTORIA_OPEN_OBJECT Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue, "this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue"); #endif Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.Metadata, "this.requestDescription.TargetKind != RequestTargetKind.Metadata"); if (this.requestDescription.TargetKind == RequestTargetKind.ServiceDirectory) { if (this.responseFormat == ContentFormat.Json) { JsonServiceDocumentSerializer serviceSerializer = new JsonServiceDocumentSerializer(stream, this.Provider, this.encoding); exceptionWriter = serviceSerializer; serviceSerializer.WriteRequest(); break; } else { AtomServiceDocumentSerializer serviceSerializer = new AtomServiceDocumentSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding); exceptionWriter = serviceSerializer; serviceSerializer.WriteRequest(); } } else { Serializer serializer; if (ContentFormat.Json == this.responseFormat) { serializer = new JsonSerializer(this.requestDescription, stream, this.AbsoluteServiceUri, this.service, this.encoding, this.Host.ResponseETag); } else if (ContentFormat.PlainXml == this.responseFormat) { serializer = new PlainXmlSerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, stream, this.encoding); } else { Debug.Assert( #if ASTORIA_OPEN_OBJECT this.requestDescription.TargetKind == RequestTargetKind.OpenProperty || #endif this.requestDescription.TargetKind == RequestTargetKind.Resource, "TargetKind " + this.requestDescription.TargetKind + " == Resource || OpenProperty -- POX should have handled it otherwise."); serializer = new SyndicationSerializer( this.requestDescription, this.AbsoluteServiceUri, this.service, stream, this.encoding, this.Host.ResponseETag, new Atom10FormatterFactory()); } exceptionWriter = serializer; Debug.Assert(exceptionWriter != null, "this.exceptionWriter != null"); serializer.WriteRequest(this.queryResults, this.hasMoved); } break; default: Debug.Assert( this.responseFormat == ContentFormat.MetadataDocument, "responseFormat(" + this.responseFormat + ") == ContentFormat.MetadataDocument -- otherwise exception should have been thrown before"); Debug.Assert(this.requestDescription.TargetKind == RequestTargetKind.Metadata, "this.requestDescription.TargetKind == RequestTargetKind.Metadata"); MetadataSerializer metadataSerializer = new MetadataSerializer(stream, this.AbsoluteServiceUri, this.Provider, this.encoding); exceptionWriter = metadataSerializer; metadataSerializer.WriteRequest(); break; } } catch (Exception exception) { if (!WebUtil.IsCatchableExceptionType(exception)) { throw; } // Only JSON and XML are supported. string contentType = (this.responseFormat == ContentFormat.Json) ? XmlConstants.MimeApplicationJson : XmlConstants.MimeApplicationXml; ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter); } finally { WebUtil.Dispose(this.queryResults); } } } } // 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
- WrappedKeySecurityToken.cs
- PropertyChangedEventManager.cs
- ListenerAdapter.cs
- ResolveMatchesCD1.cs
- SystemFonts.cs
- XmlSchemaExporter.cs
- XmlNamespaceManager.cs
- ImportDesigner.xaml.cs
- EmptyReadOnlyDictionaryInternal.cs
- EditModeSwitchButton.cs
- MimeXmlImporter.cs
- InheritanceService.cs
- DragDrop.cs
- CounterSampleCalculator.cs
- WebScriptMetadataFormatter.cs
- TabRenderer.cs
- JulianCalendar.cs
- VarRefManager.cs
- DateTimeValueSerializerContext.cs
- DashStyle.cs
- GuidConverter.cs
- FileChangesMonitor.cs
- XmlUnspecifiedAttribute.cs
- ReadOnlyHierarchicalDataSourceView.cs
- ArgIterator.cs
- DataBindingHandlerAttribute.cs
- BinaryConverter.cs
- BasicExpressionVisitor.cs
- TransactionsSectionGroup.cs
- CompensationHandlingFilter.cs
- WindowsRegion.cs
- XmlSchemaInferenceException.cs
- AutomationPropertyInfo.cs
- ellipse.cs
- UICuesEvent.cs
- DataObjectAttribute.cs
- DefinitionBase.cs
- WindowsFormsSynchronizationContext.cs
- CryptoKeySecurity.cs
- Span.cs
- PolicyStatement.cs
- PointHitTestResult.cs
- InkSerializer.cs
- oledbconnectionstring.cs
- XmlSchemaAny.cs
- RealProxy.cs
- QueryGenerator.cs
- Char.cs
- CommandManager.cs
- Model3DGroup.cs
- SplineKeyFrames.cs
- WorkflowViewElement.cs
- GraphicsContext.cs
- PostBackTrigger.cs
- RepeatEnumerable.cs
- URLBuilder.cs
- WeakEventManager.cs
- LicFileLicenseProvider.cs
- TextPatternIdentifiers.cs
- SecureEnvironment.cs
- TextViewElement.cs
- InvalidOleVariantTypeException.cs
- FlowDocumentPage.cs
- ConfigXmlElement.cs
- OutputCacheSection.cs
- LongAverageAggregationOperator.cs
- PasswordPropertyTextAttribute.cs
- TemplatedAdorner.cs
- SelectedDatesCollection.cs
- TTSEngineProxy.cs
- PropertyValidationContext.cs
- MappingMetadataHelper.cs
- ProtocolState.cs
- PointValueSerializer.cs
- SynchronizingStream.cs
- ServiceNameElement.cs
- XPathNodePointer.cs
- RuntimeConfigurationRecord.cs
- HitTestWithPointDrawingContextWalker.cs
- DataKey.cs
- TextParentUndoUnit.cs
- StopRoutingHandler.cs
- UserControl.cs
- OverflowException.cs
- ValueType.cs
- RestHandlerFactory.cs
- DesignerGenericWebPart.cs
- MSAAWinEventWrap.cs
- BitmapEffectDrawing.cs
- MissingFieldException.cs
- ExpressionTable.cs
- ServicePoint.cs
- SqlError.cs
- Char.cs
- _emptywebproxy.cs
- AdornerPresentationContext.cs
- FilteredDataSetHelper.cs
- DataListCommandEventArgs.cs
- PointConverter.cs
- NetworkInformationPermission.cs