Beispiel #1
0
 /// <summary>
 /// Dispose the update provider instance
 /// </summary>
 internal void DisposeProvider()
 {
     if (this.updateProvider != null)
     {
         WebUtil.Dispose(this.updateProvider);
         this.updateProvider = null;
     }
 }
Beispiel #2
0
        internal static void SerializeODataError(HandleExceptionArgs args, ODataMessageWriter writer, Stream outputStream, Encoding encoding)
        {
            ODataError error = CreateODataErrorFromExceptionArgs(args);

            try
            {
                writer.WriteError(error, args.UseVerboseErrors);
            }
            catch (InvalidOperationException)
            {
                if (!WebUtil.CompareMimeType(args.ResponseContentType, "application/json;odata=verbose"))
                {
                    WebUtil.Dispose(writer);
                    using (XmlWriter writer2 = XmlWriter.Create(outputStream, XmlUtil.CreateXmlWriterSettings(encoding)))
                    {
                        ErrorUtils.WriteXmlError(writer2, error, args.UseVerboseErrors, 100);
                    }
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Get the single result from the given segment info
        /// </summary>
        /// <param name="segmentInfo">segmentInfo which contains the request query</param>
        /// <returns>query result as returned by the IQueryable query</returns>
        internal static IEnumerator GetSingleResultFromEnumerable(SegmentInfo segmentInfo)
        {
            IEnumerator queryResults  = WebUtil.GetRequestEnumerator(segmentInfo.RequestEnumerable);
            bool        shouldDispose = true;

            try
            {
                WebUtil.CheckResourceExists(queryResults.MoveNext(), segmentInfo.Identifier);

                CheckQueryResult(queryResults.Current, segmentInfo);

                shouldDispose = false;
                return(queryResults);
            }
            finally
            {
                // Dispose the Enumerator in case of error
                if (shouldDispose)
                {
                    WebUtil.Dispose(queryResults);
                }
            }
        }
Beispiel #4
0
        /// <summary>Writes the request body to the specified <see cref="Stream"/>.</summary>
        /// <param name="stream">Stream to write to.</param>
        internal void Write(Stream stream)
        {
            Debug.Assert(stream != null, "stream != null");
            IExceptionWriter exceptionWriter = null;

            try
            {
                switch (this.responseFormat)
                {
                case ContentFormat.Binary:
                    Debug.Assert(
                        this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue ||
                        this.requestDescription.TargetKind == RequestTargetKind.PrimitiveValue ||
                        this.requestDescription.TargetKind == RequestTargetKind.MediaResource,
                        this.requestDescription.TargetKind + " is PrimitiveValue or OpenPropertyValue or StreamPropertyValue");

                    BinarySerializer binarySerializer = new BinarySerializer(stream);
                    exceptionWriter = binarySerializer;
                    if (this.requestDescription.TargetKind == RequestTargetKind.MediaResource)
                    {
                        Debug.Assert(this.mediaResourceStream != null, "this.mediaResourceStream != null");

                        binarySerializer.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                    }
                    else
                    {
                        binarySerializer.WriteRequest(this.queryResults.Current);
                    }

                    break;

                case ContentFormat.Text:
                    Debug.Assert(
                        this.requestDescription.TargetKind == RequestTargetKind.OpenPropertyValue ||
                        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");
                    Debug.Assert(this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue, "this.requestDescription.TargetKind != RequestTargetKind.OpenPropertyValue");
                    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(this.service);
                        }
                    }
                    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(
                                this.requestDescription.TargetKind == RequestTargetKind.OpenProperty ||
                                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(this.service);
                    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);
                WebUtil.Dispose(this.mediaResourceStream);
            }
        }
        internal void Write(Stream stream)
        {
            IExceptionWriter exceptionWriter = null;
            Stream           output          = null;
            Serializer       serializer      = null;

            System.Data.Services.ODataResponseMessage responseMessage = this.responseMessage as System.Data.Services.ODataResponseMessage;
            if (responseMessage != null)
            {
                responseMessage.SetStream(stream);
            }
            try
            {
                BinarySerializer   serializer2;
                MetadataSerializer serializer7;
                switch (this.payloadKind)
                {
                case ODataPayloadKind.Feed:
                case ODataPayloadKind.Entry:
                {
                    EntitySerializer serializer6 = new EntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.service.OperationContext.Host.ResponseETag, this.messageWriter, this.contentFormat);
                    serializer = serializer6;
                    serializer6.WriteRequest(this.queryResults, this.hasMoved);
                    return;
                }

                case ODataPayloadKind.Property:
                case ODataPayloadKind.EntityReferenceLink:
                case ODataPayloadKind.EntityReferenceLinks:
                case ODataPayloadKind.Collection:
                {
                    NonEntitySerializer serializer5 = new NonEntitySerializer(this.requestDescription, this.AbsoluteServiceUri, this.service, this.messageWriter);
                    serializer = serializer5;
                    serializer5.WriteRequest(this.queryResults, this.hasMoved);
                    return;
                }

                case ODataPayloadKind.Value:
                    new TextSerializer(this.messageWriter).WriteRequest(this.queryResults.Current);
                    return;

                case ODataPayloadKind.BinaryValue:
                    output          = this.responseMessage.GetStream();
                    serializer2     = new BinarySerializer(output);
                    exceptionWriter = serializer2;
                    if (this.requestDescription.TargetKind != RequestTargetKind.MediaResource)
                    {
                        break;
                    }
                    if (this.mediaResourceStream != null)
                    {
                        serializer2.WriteRequest(this.mediaResourceStream, this.service.StreamProvider.StreamBufferSize);
                    }
                    return;

                case ODataPayloadKind.ServiceDocument:
                    new ServiceDocumentSerializer(this.messageWriter).WriteServiceDocument(this.service.Provider);
                    return;

                default:
                    goto Label_0194;
                }
                serializer2.WriteRequest(this.queryResults.Current);
                return;

                Label_0194:
                serializer7 = new MetadataSerializer(this.messageWriter);
                serializer7.WriteMetadataDocument(this.service);
            }
            catch (Exception exception)
            {
                if (!CommonUtil.IsCatchableExceptionType(exception))
                {
                    throw;
                }
                if (serializer != null)
                {
                    serializer.Flush();
                }
                string contentType = this.responseMessage.GetHeader("Content-Type").StartsWith("application/json", StringComparison.OrdinalIgnoreCase) ? "application/json;odata=verbose" : "application/xml";
                if (this.messageWriter != null)
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, this.messageWriter, stream, this.encoding);
                }
                else
                {
                    ErrorHandler.HandleDuringWritingException(exception, this.service, contentType, exceptionWriter);
                }
            }
            finally
            {
                WebUtil.Dispose(this.messageWriter);
                WebUtil.Dispose(this.queryResults);
                WebUtil.Dispose(this.mediaResourceStream);
                if ((output != null) && (responseMessage == null))
                {
                    output.Dispose();
                }
            }
        }