Skip to content
Merged
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
Change response buffering so it doesn't depend on policy
  • Loading branch information
annelo-msft committed Jan 4, 2024
commit 56d44803dbaf0c1fd0537c5df49b1a801b707b2e
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.ClientModel.Internal;
using System.ClientModel.Primitives;
using System.Globalization;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -79,7 +80,10 @@ private static string GetMessage(string? message, PipelineResponse? response)
return DefaultMessage;
}

ResponseBufferingPolicy.BufferContent(response, ResponseBufferingPolicy.DefaultNetworkTimeout, new CancellationTokenSource());
if (!response.TryGetBufferedContent(out _))
{
BufferResponse(response);
}

StringBuilder messageBuilder = new();

Expand All @@ -103,4 +107,22 @@ private static string GetMessage(string? message, PipelineResponse? response)

return messageBuilder.ToString();
}

private static void BufferResponse(PipelineResponse response)
{
if (response.ContentStream is null)
{
return;
}

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

// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CSharp" />
<PackageReference Include="Moq" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Azure.Core\src\Azure.Core.csproj" />
<ProjectReference Include="..\src\System.ClientModel.csproj" />
<ProjectReference Include="client\System.ClientModel.Tests.Client.csproj" />
</ItemGroup>
Expand Down