/// <summary>
        /// Writes a single OData batch response.
        /// </summary>
        /// <param name="writer">The <see cref="ODataBatchWriter"/>.</param>
        /// <param name="response">The response message.</param>
        public static async Task WriteMessageAsync(ODataBatchWriter writer, HttpResponseMessage response)
        {
            if (writer == null)
            {
                throw Error.ArgumentNull("writer");
            }
            if (response == null)
            {
                throw Error.ArgumentNull("response");
            }

            ODataBatchOperationResponseMessage batchResponse = writer.CreateOperationResponseMessage();

            batchResponse.StatusCode = (int)response.StatusCode;

            foreach (KeyValuePair<string, IEnumerable<string>> header in response.Headers)
            {
                batchResponse.SetHeader(header.Key, String.Join(",", header.Value));
            }

            if (response.Content != null)
            {
                foreach (KeyValuePair<string, IEnumerable<string>> header in response.Content.Headers)
                {
                    batchResponse.SetHeader(header.Key, String.Join(",", header.Value));
                }

                using (Stream stream = batchResponse.GetStream())
                {
                    await response.Content.CopyToAsync(stream);
                }
            }
        }
Beispiel #2
0
 internal static void HandleBatchOperationError(IDataService service, DataServiceHostWrapper host, Exception exception, ODataBatchWriter batchWriter, Stream responseStream, Version defaultResponseVersion)
 {
     string str;
     Encoding encoding;
     string str2;
     Version version;
     TryGetResponseFormatForError(service, host, defaultResponseVersion, out str, out encoding, out str2, out version);
     HandleExceptionArgs args = new HandleExceptionArgs(exception, false, str2, service.Configuration.UseVerboseErrors);
     service.InternalHandleException(args);
     Action<Stream> action = null;
     if (host != null)
     {
         host.ResponseVersion = version.ToString(2) + ";";
         host.ProcessException(args);
         action = ProcessBenignException(exception, service);
     }
     if (action == null)
     {
         ODataBatchOperationResponseMessage operationResponseMessage;
         if (host != null)
         {
             operationResponseMessage = host.BatchServiceHost.GetOperationResponseMessage();
             WebUtil.SetResponseHeadersForBatchRequests(operationResponseMessage, host.BatchServiceHost);
         }
         else
         {
             operationResponseMessage = batchWriter.CreateOperationResponseMessage();
             operationResponseMessage.StatusCode = args.ResponseStatusCode;
         }
         using (ODataMessageWriter writer = ResponseBodyWriter.CreateMessageWriter(null, service, version, operationResponseMessage, str, null))
         {
             SerializeODataError(args, writer, responseStream, encoding);
         }
     }
 }