Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit fee0f67

Browse files
committed
Write sync to MemoryStream + switch to AsyncLocal in NETStd
1 parent aeb03f9 commit fee0f67

File tree

5 files changed

+41
-12
lines changed

5 files changed

+41
-12
lines changed

src/ServiceStack.Text/DefaultMemory.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,15 @@ public override Task WriteAsync(Stream stream, ReadOnlyMemory<byte> value, Cance
444444
try
445445
{
446446
value.CopyTo(bytes);
447-
return stream.WriteAsync(bytes, 0, value.Length, token);
447+
if (stream is MemoryStream ms)
448+
{
449+
ms.Write(bytes, 0, value.Length);
450+
return TypeConstants.EmptyTask;
451+
}
452+
else
453+
{
454+
return stream.WriteAsync(bytes, 0, value.Length, token);
455+
}
448456
}
449457
finally
450458
{

src/ServiceStack.Text/JsConfigScope.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Collections.Generic;
34
using ServiceStack.Text.Json;
45
using ServiceStack.Text.Jsv;
@@ -11,30 +12,42 @@ public sealed class JsConfigScope : Config, IDisposable
1112
bool disposed;
1213
readonly JsConfigScope parent;
1314

14-
[ThreadStatic]
15-
private static JsConfigScope head;
15+
#if NETSTANDARD2_0
16+
private static AsyncLocal<JsConfigScope> head = new AsyncLocal<JsConfigScope>();
17+
#else
18+
[ThreadStatic] private static JsConfigScope head;
19+
#endif
1620

1721
internal JsConfigScope()
1822
{
1923
PclExport.Instance.BeginThreadAffinity();
2024

25+
#if NETSTANDARD2_0
26+
parent = head.Value;
27+
head.Value = this;
28+
#else
2129
parent = head;
2230
head = this;
31+
#endif
2332
}
2433

25-
internal static JsConfigScope Current => head;
26-
27-
public static void DisposeCurrent()
28-
{
29-
head?.Dispose();
30-
}
34+
internal static JsConfigScope Current =>
35+
#if NETSTANDARD2_0
36+
head.Value;
37+
#else
38+
head;
39+
#endif
3140

3241
public void Dispose()
3342
{
3443
if (!disposed)
3544
{
3645
disposed = true;
46+
#if NETSTANDARD2_0
47+
head.Value = parent;
48+
#else
3749
head = parent;
50+
#endif
3851

3952
PclExport.Instance.EndThreadAffinity();
4053
}

src/ServiceStack.Text/LicenseUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public static void RegisterLicense(string licenseKeyText)
219219
var key = VerifyLicenseKeyText(licenseKeyText);
220220
ValidateLicenseKey(key);
221221
}
222-
catch (PlatformNotSupportedException pse)
222+
catch (PlatformNotSupportedException)
223223
{
224224
// Allow usage in environments like dotnet script
225225
__activatedLicense = new __ActivatedLicense(new LicenseKey { Type = LicenseType.Indie });

src/ServiceStack.Text/NetCoreMemory.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,14 @@ public override Task WriteAsync(Stream stream, ReadOnlySpan<char> value, Cancell
107107

108108
public override async Task WriteAsync(Stream stream, ReadOnlyMemory<byte> value, CancellationToken token = default)
109109
{
110-
await stream.WriteAsync(value, token);
110+
if (stream is MemoryStream ms)
111+
{
112+
ms.Write(value.Span);
113+
}
114+
else
115+
{
116+
await stream.WriteAsync(value, token);
117+
}
111118
}
112119

113120
public override object Deserialize(Stream stream, Type type, DeserializeStringSpanDelegate deserializer)

src/ServiceStack.Text/StreamExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ public static Task WriteAsync(this Stream stream, ReadOnlyMemory<byte> value, Ca
317317
MemoryProvider.Instance.WriteAsync(stream, value, token);
318318

319319
[MethodImpl(MethodImplOptions.AggressiveInlining)]
320-
public static Task WriteAsync(this Stream stream, byte[] bytes, CancellationToken token = default) => stream.WriteAsync(bytes, 0, bytes.Length, token);
320+
public static Task WriteAsync(this Stream stream, byte[] bytes, CancellationToken token = default) =>
321+
MemoryProvider.Instance.WriteAsync(stream, bytes, token);
321322

322323
[MethodImpl(MethodImplOptions.AggressiveInlining)]
323324
public static Task CopyToAsync(this Stream input, Stream output, CancellationToken token = default) => input.CopyToAsync(output, AsyncBufferSize, token);

0 commit comments

Comments
 (0)