Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Another idea that doesn't work
  • Loading branch information
annelo-msft committed Jan 9, 2024
commit 14f6cd5658c2126eda076d86b8484e89c172c263
32 changes: 16 additions & 16 deletions sdk/core/Azure.Core/src/RequestFailedException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont

private static ErrorDetails CreateRequestFailedExceptionContent(Response response, RequestFailedDetailsParser? parser)
{
response.Buffer();
BufferResponseIfNeeded(response);

parser ??= response.RequestFailedDetailsParser;

Expand Down Expand Up @@ -224,24 +224,24 @@ private static ErrorDetails CreateRequestFailedExceptionContent(Response respons
return new ErrorDetails(messageBuilder.ToString(), error?.Code, additionalInfo);
}

//private static void BufferResponseIfNeeded(Response response)
//{
// // Buffer into a memory stream if not already buffered
// if (response.ContentStream is null or MemoryStream)
// {
// return;
// }
private static void BufferResponseIfNeeded(Response response)
{
// Buffer into a memory stream if not already buffered
if (response.ContentStream is null or MemoryStream)
{
return;
}

// var bufferedStream = new MemoryStream();
// response.ContentStream.CopyTo(bufferedStream);
var bufferedStream = new MemoryStream();
response.ContentStream.CopyTo(bufferedStream);

// // Dispose the unbuffered stream
// response.ContentStream.Dispose();
// Dispose the unbuffered stream
response.ContentStream.Dispose();

// // Reset the position of the buffered stream and set it on the response
// bufferedStream.Position = 0;
// response.ContentStream = bufferedStream;
//}
// Reset the position of the buffered stream and set it on the response
bufferedStream.Position = 0;
response.ContentStream = bufferedStream;
}

// This class needs to be internal rather than private so that it can be used
// by the System.Text.Json source generator.
Expand Down
2 changes: 0 additions & 2 deletions sdk/core/Azure.Core/src/Response.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ public override string ToString()
return $"Status: {Status}, ReasonPhrase: {ReasonPhrase}";
}

internal void Buffer() => BufferContent();

internal static void DisposeStreamIfNotBuffered(ref Stream? stream)
{
// We want to keep the ContentStream readable
Expand Down
8 changes: 6 additions & 2 deletions sdk/core/System.ClientModel/src/Message/PipelineResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public virtual BinaryData Content
{
get
{
if (_contentStream is not null)
{
return BinaryData.FromStream(_contentStream);
}

if (_content is null)
{
throw new InvalidOperationException($"The response is not fully buffered.");
Expand Down Expand Up @@ -96,8 +101,7 @@ public virtual BinaryData Content
// Same value as Stream.CopyTo uses by default
private const int DefaultCopyBufferSize = 81920;

// TODO: Prefer an approach that doesn't make this visible outside the dll
protected internal void BufferContent(TimeSpan? timeout = default, CancellationTokenSource? cts = default)
internal void BufferContent(TimeSpan? timeout = default, CancellationTokenSource? cts = default)
{
if (IsBuffered)
{
Expand Down