/// <summary>
        /// Writes the specified problem details object to the HTTP response stream.
        /// </summary>
        /// <param name="problemDetails">
        ///   The problem details.
        /// </param>
        /// <param name="response">
        ///   The HTTP response.
        /// </param>
        /// <param name="stream">
        ///   The destination response stream.
        /// </param>
        /// <returns>
        ///   A <see cref="Task"/> that will perform the write.
        /// </returns>
        private async Task WriteProblemDetailsToStream(ProblemDetails problemDetails, IOwinResponse response, Stream stream)
        {
            // Set response status code
            if (problemDetails.Status.HasValue)
            {
                response.StatusCode = problemDetails.Status.Value;
            }
            ;

            // Create a new buffer, and then create and serialize a problem details object
            // into the new buffer.
            using (var ms = new MemoryStream()) {
                var content = new ObjectContent(
                    problemDetails.GetType(),
                    problemDetails,
                    new JsonMediaTypeFormatter(),
                    ClientErrorDataDefaults.MediaType
                    );
                await content.CopyToAsync(ms).ConfigureAwait(false);

                // Now set the content type and content length on the response, and copy
                // the content of the new buffer into the original response stream.
                response.ContentType   = content.Headers.ContentType.ToString();
                response.ContentLength = ms.Length;
                ms.Position            = 0;
                await ms.CopyToAsync(stream).ConfigureAwait(false);
            }
        }
예제 #2
0
        /// <summary>
        /// For the given Submit response, serialize and deserialize the content. This forces the
        /// formatter pipeline to run so we can verify that registered serializers are being used
        /// properly.
        /// </summary>
        private ChangeSetEntry[] GetChangesetResponse(HttpResponseMessage responseMessage)
        {
            // serialize the content to a stream
            ObjectContent content = (ObjectContent)responseMessage.Content;
            MemoryStream  ms      = new MemoryStream();

            content.CopyToAsync(ms).Wait();
            ms.Flush();
            ms.Seek(0, 0);

            // deserialize based on content type
            ChangeSetEntry[] changeSet = null;
            string           mediaType = responseMessage.RequestMessage.Content.Headers.ContentType.MediaType;

            if (mediaType == "application/json")
            {
                JsonSerializer ser = new JsonSerializer()
                {
                    PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.All
                };
                changeSet = (ChangeSetEntry[])ser.Deserialize(new JsonTextReader(new StreamReader(ms)), content.ObjectType);
            }
            else
            {
                DataContractSerializer ser = new DataContractSerializer(content.ObjectType, GetTestKnownTypes());
                changeSet = (ChangeSetEntry[])ser.ReadObject(ms);
            }

            return(changeSet);
        }
예제 #3
0
        // The content is written async - hence declare as async task
        public static async Task LogMessage(String message, ObjectContent content, String url, LoggingLevel level)
        {
            if ((logToEventLog && level <= logToEventLevel) || (logToFileLog && level <= logToFileLevel))
            {
                MemoryStream stream = new MemoryStream();
                await content.CopyToAsync(stream);

                stream.Position = 0;
                StreamReader reader        = new StreamReader(stream);
                String       contentString = reader.ReadToEnd();

                LogMessage(message + url + "\nContent:\n" + contentString, level);
            }
        }