Skip to content

Commit 53ef3e0

Browse files
authored
Use System.Text.Json in Netfx build of MSBuildSdkResolver (#39573)
### Summary Switch the .NET Framework version of `MSBuildSdkResolver` to use `System.Text.Json` instead of `Newtonsoft.Json` to align it with MSBuild and make it possible to load the resolver with `Assembly.Load`. ### Notes We cannot build against the SDK version of S.T.J because that's generally not available in MSBuild. I have set the version to 8.0.0 and I believe it can stay like this for as long as we don't need any newer functionality from the library. When the need to upgrade arises, we should just check that MSBuild in VS where this is inserted to has the new (or higher) version bundled with it.
1 parent fb04b17 commit 53ef3e0

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/Resolvers/Microsoft.DotNet.MSBuildSdkResolver/Microsoft.DotNet.MSBuildSdkResolver.csproj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
<Nullable>Enable</Nullable>
1515

16-
<UseSystemTextJson Condition="'$(TargetFramework)'!='netstandard2.0' And '$(TargetFramework)'!='net472'">True</UseSystemTextJson>
16+
<UseSystemTextJson Condition="'$(TargetFramework)'!='netstandard2.0'">True</UseSystemTextJson>
1717
<DefineConstants Condition="'$(UseSystemTextJson)'=='True'">$(DefineConstants);USE_SYSTEM_TEXT_JSON</DefineConstants>
1818

19+
<!-- Netfx version of the resolver builds against the lowest version of System.Text.Json that's guaranteed to be shipped with MSBuild in VS -->
20+
<SystemTextJsonVersionOverride>8.0.0</SystemTextJsonVersionOverride>
21+
1922
<!-- Create FileDefinitions items for ResolveHostfxrCopyLocalContent target -->
2023
<EmitLegacyAssetsFileItems>true</EmitLegacyAssetsFileItems>
2124

@@ -100,7 +103,8 @@
100103
</ItemGroup>
101104

102105
<ItemGroup>
103-
<!-- No PackageReference to System.Text.Json necessary, because it's included in .NET 5.0 and higher -->
106+
<!-- No PackageReference to System.Text.Json necessary when targeting .NET, because it's included in .NET 5.0 and higher -->
107+
<PackageReference Include="System.Text.Json" VersionOverride="$(SystemTextJsonVersionOverride)" Condition="'$(UseSystemTextJson)'=='True' and '$(TargetFramework)'=='net472'"/>
104108
<PackageReference Include="Newtonsoft.Json" Condition="'$(UseSystemTextJson)'!='True'"/>
105109

106110
<!-- Reference this package to avoid package downgrade errors. See https://github.com/dotnet/sdk/issues/3044 for details -->
@@ -139,13 +143,16 @@
139143

140144
<ItemGroup>
141145
<ExpectedDependencies Include="Microsoft.Deployment.DotNet.Releases, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
142-
<ExpectedDependencies Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" />
146+
<ExpectedDependencies Include="System.Text.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
147+
<ExpectedDependencies Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
143148
<ExpectedDependencies Include="Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
144149
<ExpectedDependencies Include="System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
145150
<ExpectedDependencies Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
146151
<ExpectedDependencies Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
147152
<ExpectedDependencies Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
148153
<ExpectedDependencies Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
154+
<ExpectedDependencies Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
155+
<ExpectedDependencies Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
149156
</ItemGroup>
150157

151158
<!-- Check that the dependencies of the output assembly match our expectations -->

src/Resolvers/Microsoft.NET.Sdk.WorkloadManifestReader/WorkloadManifestReader.SystemTextJson.cs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33

44
#if USE_SYSTEM_TEXT_JSON
55

6-
using System;
76
using System.Buffers;
8-
using System.Collections.Generic;
9-
using System.IO;
107
using System.Text.Json;
118

129
namespace Microsoft.NET.Sdk.WorkloadManifestReader
@@ -47,17 +44,17 @@ internal ref struct Utf8JsonStreamReader
4744
Utf8JsonReader reader;
4845
readonly Stream stream;
4946

50-
IMemoryOwner<byte> buffer;
47+
byte[]? buffer;
5148

5249
Span<byte> span;
5350

5451
public Utf8JsonStreamReader(Stream stream, JsonReaderOptions readerOptions)
5552
{
5653
this.stream = stream;
5754

58-
buffer = MemoryPool<byte>.Shared.Rent(segmentSize);
59-
var readCount = stream.Read(buffer.Memory.Span);
60-
span = buffer.Memory.Slice(0, readCount).Span;
55+
buffer = ArrayPool<byte>.Shared.Rent(segmentSize);
56+
var readCount = stream.Read(buffer, 0, buffer.Length);
57+
span = buffer.AsSpan().Slice(0, readCount);
6158

6259
if (span.StartsWith(utf8Bom))
6360
{
@@ -73,6 +70,11 @@ public bool Read()
7370
{
7471
if (reader.IsFinalBlock)
7572
{
73+
if (buffer != null)
74+
{
75+
ArrayPool<byte>.Shared.Return(buffer);
76+
buffer = null;
77+
}
7678
return false;
7779
}
7880

@@ -86,18 +88,21 @@ public bool Read()
8688

8789
int remaining = (int)(span.Length - reader.BytesConsumed);
8890

89-
var newBuffer = MemoryPool<byte>.Shared.Rent(newSegmentSize);
91+
var newBuffer = ArrayPool<byte>.Shared.Rent(newSegmentSize);
9092

9193
if (remaining > 0)
9294
{
93-
span.Slice((int)reader.BytesConsumed).CopyTo(newBuffer.Memory.Span);
95+
span.Slice((int)reader.BytesConsumed).CopyTo(newBuffer);
9496
}
9597

96-
var readCount = stream.Read(newBuffer.Memory.Span.Slice(remaining));
98+
var readCount = stream.Read(newBuffer, remaining, newBuffer.Length - remaining);
9799

98-
buffer.Dispose();
100+
if (buffer != null)
101+
{
102+
ArrayPool<byte>.Shared.Return(buffer);
103+
}
99104
buffer = newBuffer;
100-
span = newBuffer.Memory.Slice(0, remaining + readCount).Span;
105+
span = newBuffer.AsSpan().Slice(0, remaining + readCount);
101106

102107
reader = new Utf8JsonReader(span, stream.Position >= stream.Length, reader.CurrentState);
103108
}

0 commit comments

Comments
 (0)