Exemple #1
0
        /// <inheritdoc/>
        public override async Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (graph == null)
            {
                throw Error.ArgumentNull(nameof(graph));
            }

            if (messageWriter == null)
            {
                throw Error.ArgumentNull(nameof(messageWriter));
            }

            ODataError oDataError = graph as ODataError;

            if (oDataError == null)
            {
                if (!IsHttpError(graph))
                {
                    throw new SerializationException(
                              Error.Format(SRResources.ErrorTypeMustBeODataErrorOrHttpError, graph.GetType().FullName));
                }
                else
                {
                    oDataError = CreateODataError(graph);
                }
            }

            bool includeDebugInformation = oDataError.InnerError != null;
            await messageWriter.WriteErrorAsync(oDataError, includeDebugInformation).ConfigureAwait(false);
        }
Exemple #2
0
        /// <inheritdoc/>
        public override Task WriteObjectAsync(object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
        {
            if (graph == null)
            {
                throw Error.ArgumentNull("graph");
            }
            if (messageWriter == null)
            {
                throw Error.ArgumentNull("messageWriter");
            }

            ODataError oDataError = GetError(graph);
            bool       includeDebugInformation = oDataError.InnerError != null;

            return(messageWriter.WriteErrorAsync(oDataError, includeDebugInformation));
        }
Exemple #3
0
        public async Task WriteInStreamError_APIsShouldYieldSameResult()
        {
            var nestedWriterSettings = new ODataMessageWriterSettings
            {
                Version = ODataVersion.V4,
                EnableMessageStreamDisposal = false
            };

            nestedWriterSettings.SetServiceDocumentUri(new Uri(ServiceUri));

            var asyncException = await Assert.ThrowsAsync <ODataException>(async() =>
            {
                IODataResponseMessage asyncResponseMessage = new InMemoryMessage {
                    StatusCode = 200, Stream = this.asyncStream
                };
                using (var messageWriter = new ODataMessageWriter(asyncResponseMessage, writerSettings))
                {
                    // Call to CreateODataAsynchronousWriterAsync triggers setting of output in-stream error listener
                    var asynchronousWriter     = await messageWriter.CreateODataAsynchronousWriterAsync();
                    var responseMessage        = await asynchronousWriter.CreateResponseMessageAsync();
                    responseMessage.StatusCode = 200;

                    // Next section added is to demonstrate that what was already written is flushed to the buffer before exception is thrown
                    using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings))
                    {
                        var writer = await nestedMessageWriter.CreateODataResourceWriterAsync();
                    }

                    await messageWriter.WriteErrorAsync(
                        new ODataError {
                        ErrorCode = "NRE", Message = "Object reference not set to an instance of an object."
                    },
                        /*includeDebugInformation*/ true);
                }
            });

            this.asyncStream.Position = 0;
            var asyncResult = await new StreamReader(this.asyncStream).ReadToEndAsync();

            var syncException = await Assert.ThrowsAsync <ODataException>(
                () => TaskUtils.GetTaskForSynchronousOperation(() =>
            {
                IODataResponseMessage syncResponseMessage = new InMemoryMessage {
                    StatusCode = 200, Stream = this.syncStream
                };
                using (var messageWriter = new ODataMessageWriter(syncResponseMessage, writerSettings))
                {
                    // Call to CreateODataAsynchronousWriterAsync triggers setting of output in-stream error listener
                    var asynchronousWriter     = messageWriter.CreateODataAsynchronousWriter();
                    var responseMessage        = asynchronousWriter.CreateResponseMessage();
                    responseMessage.StatusCode = 200;

                    // Next section is added to demonstrate that what was already written is flushed to the buffer before exception is thrown
                    using (var nestedMessageWriter = new ODataMessageWriter(responseMessage, nestedWriterSettings))
                    {
                        var writer = nestedMessageWriter.CreateODataResourceWriter();
                    }

                    messageWriter.WriteError(
                        new ODataError {
                        ErrorCode = "NRE", Message = "Object reference not set to an instance of an object."
                    },
                        /*includeDebugInformation*/ true);
                }
            }));

            this.syncStream.Position = 0;
            var syncResult = await new StreamReader(this.syncStream).ReadToEndAsync();

            var expected = @"HTTP/1.1 200 OK
OData-Version: 4.0
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8

";

            Assert.Equal(Strings.ODataAsyncWriter_CannotWriteInStreamErrorForAsync, asyncException.Message);
            Assert.Equal(Strings.ODataAsyncWriter_CannotWriteInStreamErrorForAsync, syncException.Message);
            Assert.Equal(expected, asyncResult);
            Assert.Equal(expected, syncResult);
        }