update responses

This commit is contained in:
Luke Pulverenti 2016-11-12 02:45:06 -05:00
parent a855864207
commit 6865bb5353
3 changed files with 27 additions and 12 deletions

View File

@ -18,7 +18,7 @@ namespace ServiceStack.Host
serializer(response, responseStream); serializer(response, responseStream);
} }
private Action<object, Stream> GetStreamSerializer(string contentType) public static Action<object, Stream> GetStreamSerializer(string contentType)
{ {
switch (GetRealContentType(contentType)) switch (GetRealContentType(contentType))
{ {

View File

@ -15,19 +15,19 @@ namespace ServiceStack
{ {
public static class HttpResponseExtensionsInternal public static class HttpResponseExtensionsInternal
{ {
public static async Task<bool> WriteToOutputStream(IResponse response, Stream outputStream, object result) public static async Task<bool> WriteToOutputStream(IResponse response, object result)
{ {
var asyncStreamWriter = result as IAsyncStreamWriter; var asyncStreamWriter = result as IAsyncStreamWriter;
if (asyncStreamWriter != null) if (asyncStreamWriter != null)
{ {
await asyncStreamWriter.WriteToAsync(outputStream, CancellationToken.None).ConfigureAwait(false); await asyncStreamWriter.WriteToAsync(response.OutputStream, CancellationToken.None).ConfigureAwait(false);
return true; return true;
} }
var streamWriter = result as IStreamWriter; var streamWriter = result as IStreamWriter;
if (streamWriter != null) if (streamWriter != null)
{ {
streamWriter.WriteTo(outputStream); streamWriter.WriteTo(response.OutputStream);
return true; return true;
} }
@ -36,7 +36,7 @@ namespace ServiceStack
{ {
using (stream) using (stream)
{ {
await stream.CopyToAsync(outputStream).ConfigureAwait(false); await stream.CopyToAsync(response.OutputStream).ConfigureAwait(false);
return true; return true;
} }
} }
@ -47,7 +47,7 @@ namespace ServiceStack
response.ContentType = "application/octet-stream"; response.ContentType = "application/octet-stream";
response.SetContentLength(bytes.Length); response.SetContentLength(bytes.Length);
await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
return true; return true;
} }
@ -152,9 +152,7 @@ namespace ServiceStack
response.ContentType += "; charset=utf-8"; response.ContentType += "; charset=utf-8";
} }
var outputStream = response.OutputStream; var writeToOutputStreamResult = await WriteToOutputStream(response, result).ConfigureAwait(false);
var writeToOutputStreamResult = await WriteToOutputStream(response, outputStream, result).ConfigureAwait(false);
if (writeToOutputStreamResult) if (writeToOutputStreamResult)
{ {
return; return;
@ -167,11 +165,28 @@ namespace ServiceStack
response.ContentType = defaultContentType; response.ContentType = defaultContentType;
var bytes = Encoding.UTF8.GetBytes(responseText); var bytes = Encoding.UTF8.GetBytes(responseText);
await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false); await response.OutputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
return; return;
} }
ContentTypes.Instance.SerializeToStream(request, result, outputStream); await WriteObject(request, result, response).ConfigureAwait(false);
}
public static async Task WriteObject(IRequest request, object result, IResponse response)
{
var contentType = request.ResponseContentType;
var serializer = ContentTypes.GetStreamSerializer(contentType);
using (var ms = new MemoryStream())
{
serializer(result, ms);
ms.Position = 0;
response.SetContentLength(ms.Length);
await ms.CopyToAsync(response.OutputStream).ConfigureAwait(false);
}
//serializer(result, outputStream);
} }
} }
} }

View File

@ -55,7 +55,7 @@ namespace ServiceStack
return; return;
} }
ContentTypes.Instance.SerializeToStream(this.RequestContext, this.Response, responseStream); await HttpResponseExtensionsInternal.WriteObject(this.RequestContext, this.Response, response).ConfigureAwait(false);
} }
} }
} }