Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netstandard 2.1 support for Orleans.Serialization #8222

Merged
merged 9 commits into from
Jan 16, 2023
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Build
run: dotnet build
- uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -49,6 +53,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -85,6 +93,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -117,6 +129,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -153,6 +169,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -187,6 +207,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&Category=${{ matrix.suite }}" --framework ${{ matrix.framework }} --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -220,6 +244,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -253,6 +281,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down Expand Up @@ -287,6 +319,10 @@ jobs:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: |
3.1.x
7.0.x
- name: Test
run: dotnet test --filter "Category=${{ matrix.provider }}&(Category=BVT|Category=SlowBVT|Category=Functional)" --blame-hang-timeout 10m --logger "trx" -- -parallel none -noshadow
env:
Expand Down
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageVersion Include="System.IO.Pipelines" Version="7.0.0" />
<PackageVersion Include="System.Memory.Data" Version="7.0.0" />
<PackageVersion Include="System.Net.NameResolution" Version="4.3.0" />
<PackageVersion Include="System.Collections.Immutable" Version="7.0.0" />
<!-- Microsoft packages -->
<PackageVersion Include="Microsoft.Build" Version="17.3.2" />
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
Expand All @@ -27,6 +28,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="$(MicrosoftCodeAnalysisVersion)" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" />
<PackageVersion Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageVersion Include="Microsoft.AspNetCore.Connections.Abstractions" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(MicrosoftExtensionsConfigurationVersion)" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsConfigurationVersion)" />
Expand Down
29 changes: 15 additions & 14 deletions src/Orleans.CodeGenerator/LibraryTypes.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -66,17 +67,17 @@ public static LibraryTypes FromCompilation(Compilation compilation, CodeGenerato
Task_1 = Type("System.Threading.Tasks.Task`1"),
Type = Type("System.Type"),
Uri = Type("System.Uri"),
Int128 = Type("System.Int128"),
UInt128 = Type("System.UInt128"),
Half = Type("System.Half"),
DateOnly = Type("System.DateOnly"),
Int128 = TypeOrDefault("System.Int128"),
UInt128 = TypeOrDefault("System.UInt128"),
Half = TypeOrDefault("System.Half"),
DateOnly = TypeOrDefault("System.DateOnly"),
DateTimeOffset = Type("System.DateTimeOffset"),
BitVector32 = Type("System.Collections.Specialized.BitVector32"),
Guid = Type("System.Guid"),
CompareInfo = Type("System.Globalization.CompareInfo"),
CultureInfo = Type("System.Globalization.CultureInfo"),
Version = Type("System.Version"),
TimeOnly = Type("System.TimeOnly"),
TimeOnly = TypeOrDefault("System.TimeOnly"),
ICodecProvider = Type("Orleans.Serialization.Serializers.ICodecProvider"),
ValueSerializer = Type("Orleans.Serialization.Serializers.IValueSerializer`1"),
ValueTask = Type("System.Threading.Tasks.ValueTask"),
Expand All @@ -86,7 +87,7 @@ public static LibraryTypes FromCompilation(Compilation compilation, CodeGenerato
Writer = Type("Orleans.Serialization.Buffers.Writer`1"),
FSharpSourceConstructFlagsOrDefault = TypeOrDefault("Microsoft.FSharp.Core.SourceConstructFlags"),
FSharpCompilationMappingAttributeOrDefault = TypeOrDefault("Microsoft.FSharp.Core.CompilationMappingAttribute"),
StaticCodecs = new WellKnownCodecDescription[]
StaticCodecs = new List<WellKnownCodecDescription>
{
new(compilation.GetSpecialType(SpecialType.System_Object), Type("Orleans.Serialization.Codecs.ObjectCodec")),
new(compilation.GetSpecialType(SpecialType.System_Boolean), Type("Orleans.Serialization.Codecs.BoolCodec")),
Expand All @@ -107,19 +108,19 @@ public static LibraryTypes FromCompilation(Compilation compilation, CodeGenerato
new(compilation.GetSpecialType(SpecialType.System_DateTime), Type("Orleans.Serialization.Codecs.DateTimeCodec")),
new(Type("System.TimeSpan"), Type("Orleans.Serialization.Codecs.TimeSpanCodec")),
new(Type("System.DateTimeOffset"), Type("Orleans.Serialization.Codecs.DateTimeOffsetCodec")),
new(Type("System.DateOnly"), Type("Orleans.Serialization.Codecs.DateOnlyCodec")),
new(Type("System.TimeOnly"), Type("Orleans.Serialization.Codecs.TimeOnlyCodec")),
new(TypeOrDefault("System.DateOnly"), TypeOrDefault("Orleans.Serialization.Codecs.DateOnlyCodec")),
new(TypeOrDefault("System.TimeOnly"), TypeOrDefault("Orleans.Serialization.Codecs.TimeOnlyCodec")),
new(Type("System.Guid"), Type("Orleans.Serialization.Codecs.GuidCodec")),
new(Type("System.Type"), Type("Orleans.Serialization.Codecs.TypeSerializerCodec")),
new(Type("System.ReadOnlyMemory`1").Construct(compilation.GetSpecialType(SpecialType.System_Byte)), Type("Orleans.Serialization.Codecs.ReadOnlyMemoryOfByteCodec")),
new(Type("System.Memory`1").Construct(compilation.GetSpecialType(SpecialType.System_Byte)), Type("Orleans.Serialization.Codecs.MemoryOfByteCodec")),
new(Type("System.Net.IPAddress"), Type("Orleans.Serialization.Codecs.IPAddressCodec")),
new(Type("System.Net.IPEndPoint"), Type("Orleans.Serialization.Codecs.IPEndPointCodec")),
new(Type("System.UInt128"), Type("Orleans.Serialization.Codecs.UInt128Codec")),
new(Type("System.Int128"), Type("Orleans.Serialization.Codecs.Int128Codec")),
new(Type("System.Half"), Type("Orleans.Serialization.Codecs.HalfCodec")),
new(TypeOrDefault("System.UInt128"), TypeOrDefault("Orleans.Serialization.Codecs.UInt128Codec")),
new(TypeOrDefault("System.Int128"), TypeOrDefault("Orleans.Serialization.Codecs.Int128Codec")),
new(TypeOrDefault("System.Half"), TypeOrDefault("Orleans.Serialization.Codecs.HalfCodec")),
new(Type("System.Uri"), Type("Orleans.Serialization.Codecs.UriCodec")),
},
}.Where(desc => desc.UnderlyingType is {} && desc.CodecType is {}).ToArray(),
WellKnownCodecs = new WellKnownCodecDescription[]
{
new(Type("System.Exception"), Type("Orleans.Serialization.ExceptionCodec")),
Expand Down Expand Up @@ -266,7 +267,7 @@ INamedTypeSymbol TypeOrDefault(string metadataName)
private INamedTypeSymbol UInt128;
private INamedTypeSymbol Half;
private INamedTypeSymbol[] _regularShallowCopyableTypes;
private INamedTypeSymbol[] RegularShallowCopyableType => _regularShallowCopyableTypes ??= new[]
private INamedTypeSymbol[] RegularShallowCopyableType => _regularShallowCopyableTypes ??= new List<INamedTypeSymbol>
{
TimeSpan,
DateOnly,
Expand All @@ -285,7 +286,7 @@ INamedTypeSymbol TypeOrDefault(string metadataName)
UInt128,
Int128,
Half
};
}.Where(t => t is {}).ToArray();

public INamedTypeSymbol[] ImmutableAttributes { get; private set; }
public INamedTypeSymbol Exception { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Microsoft.Orleans.Serialization.FSharp</PackageId>
<TargetFrameworks>$(DefaultTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>$(DefaultTargetFrameworks);netstandard2.1</TargetFrameworks>
<PackageDescription>F# core type support for Orleans.Serialization</PackageDescription>
<OrleansBuildTimeCodeGen>true</OrleansBuildTimeCodeGen>
<IsOrleansFrameworkPart>false</IsOrleansFrameworkPart>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Microsoft.Orleans.Serialization.NewtonsoftJson</PackageId>
<TargetFrameworks>$(DefaultTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>$(DefaultTargetFrameworks);netstandard2.1</TargetFrameworks>
<PackageDescription>Newtonsoft.Json integration for Orleans.Serialization</PackageDescription>
<OrleansBuildTimeCodeGen>true</OrleansBuildTimeCodeGen>
<IsOrleansFrameworkPart>false</IsOrleansFrameworkPart>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Microsoft.Orleans.Serialization.SystemTextJson</PackageId>
<TargetFrameworks>$(DefaultTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>$(DefaultTargetFrameworks);netstandard2.1</TargetFrameworks>
<PackageDescription>System.Text.Json integration for Orleans.Serialization</PackageDescription>
<OrleansBuildTimeCodeGen>true</OrleansBuildTimeCodeGen>
<IsOrleansFrameworkPart>false</IsOrleansFrameworkPart>
Expand Down
6 changes: 5 additions & 1 deletion src/Orleans.Serialization.TestKit/CopierTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public abstract class CopierTester<TValue, TCopier> where TCopier : class, IDeep

protected CopierTester(ITestOutputHelper output)
{
#if NET6_0_OR_GREATER
var seed = Random.Shared.Next();
#else
var seed = new Random().Next();
#endif
output.WriteLine($"Random seed: {seed}");
Random = new(seed);
var services = new ServiceCollection();
Expand Down Expand Up @@ -51,7 +55,7 @@ protected virtual void Configure(ISerializerBuilder builder)
protected virtual bool Equals(TValue left, TValue right) => EqualityComparer<TValue>.Default.Equals(left, right);

protected virtual Action<Action<TValue>> ValueProvider { get; }

[Fact]
public void CopiedValuesAreEqual()
{
Expand Down
8 changes: 6 additions & 2 deletions src/Orleans.Serialization.TestKit/FieldCodecTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public abstract class FieldCodecTester<TValue, TCodec> : IDisposable where TCode

protected FieldCodecTester(ITestOutputHelper output)
{
#if NET6_0_OR_GREATER
var seed = Random.Shared.Next();
#else
var seed = new Random().Next();
#endif
output.WriteLine($"Random seed: {seed}");
Random = new(seed);
var services = new ServiceCollection();
Expand Down Expand Up @@ -121,7 +125,7 @@ public void CorrectlyAdvancesReferenceCounterStream()
public void CorrectlyAdvancesReferenceCounter()
{
var pipe = new Pipe();
using var writerSession = _sessionPool.GetSession();
using var writerSession = _sessionPool.GetSession();
var writer = Writer.Create(pipe.Writer, writerSession);
var writerCodec = CreateCodec();
var beforeReference = writer.Session.ReferencedObjects.CurrentReferenceId;
Expand All @@ -139,7 +143,7 @@ public void CorrectlyAdvancesReferenceCounter()
pipe.Writer.Complete();

_ = pipe.Reader.TryRead(out var readResult);
using var readerSession = _sessionPool.GetSession();
using var readerSession = _sessionPool.GetSession();
var reader = Reader.Create(readResult.Buffer, readerSession);

var previousPos = reader.Position;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<PackageId>Microsoft.Orleans.Serialization.TestKit</PackageId>
<TargetFrameworks>$(DefaultTargetFrameworks)</TargetFrameworks>
<TargetFrameworks>$(DefaultTargetFrameworks);netstandard2.1</TargetFrameworks>
<PackageDescription>Test kit for projects using Orleans.Serialization</PackageDescription>
<IsOrleansFrameworkPart>false</IsOrleansFrameworkPart>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Orleans.Serialization/Activators/DefaultActivator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static Func<T> Init()
var il = method.GetILGenerator();
il.Emit(OpCodes.Newobj, ctor);
il.Emit(OpCodes.Ret);
return method.CreateDelegate<Func<T>>();
return (Func<T>)method.CreateDelegate(typeof(Func<T>));
}

public T Create() => _constructor is { } ctor ? ctor() : Unsafe.As<T>(FormatterServices.GetUninitializedObject(_type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ private SequenceSegment(int length)
{
if (length <= MinimumBlockSize)
{
#if NET6_0_OR_GREATER
var pinnedArray = GC.AllocateUninitializedArray<byte>(MinimumBlockSize, pinned: true);
#else
// Note: Not actually pinned in this case since it just a potential fragmentation optimization
var pinnedArray = new byte[MinimumBlockSize];
#endif
Array = pinnedArray;
}
else
Expand All @@ -218,20 +223,24 @@ private SequenceSegment(int length)

public Memory<byte> AsMemory(int offset)
{
#if NET6_0_OR_GREATER
if (IsStandardSize)
{
return MemoryMarshal.CreateFromPinnedArray(Array, offset, Array.Length);
}
#endif

return Array.AsMemory(offset);
}

public Memory<byte> AsMemory(int offset, int length)
{
#if NET6_0_OR_GREATER
if (IsStandardSize)
{
return MemoryMarshal.CreateFromPinnedArray(Array, offset, length);
}
#endif

return Array.AsMemory(offset, length);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Orleans.Serialization/Buffers/BufferWriterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ public static class BufferWriterExtensions
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Writer<TBufferWriter> CreateWriter<TBufferWriter>(this TBufferWriter buffer, SerializerSession session) where TBufferWriter : IBufferWriter<byte>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(session);
#else
if (session is null) throw new ArgumentNullException(nameof(session));
#endif
return Writer.Create(buffer, session);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/Orleans.Serialization/Cloning/IDeepCopier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ internal static class ShallowCopyableTypes
{
[typeof(decimal)] = true,
[typeof(DateTime)] = true,
#if NET6_0_OR_GREATER
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These conditions aren't going to work as expected on .NET 5 or 6 because we don't have a build target for these frameworks and so these conditions will be always false when running on .net3.1 - 6.0.

[typeof(DateOnly)] = true,
[typeof(TimeOnly)] = true,
#endif
[typeof(DateTimeOffset)] = true,
[typeof(TimeSpan)] = true,
[typeof(IPAddress)] = true,
Expand All @@ -288,9 +290,13 @@ internal static class ShallowCopyableTypes
[typeof(CultureInfo)] = true,
[typeof(Version)] = true,
[typeof(Uri)] = true,
#if NET7_0_OR_GREATER
[typeof(UInt128)] = true,
[typeof(Int128)] = true,
#endif
#if NET5_0_OR_GREATER
[typeof(Half)] = true,
#endif
};

public static bool Contains(Type type)
Expand Down Expand Up @@ -365,7 +371,7 @@ internal sealed class UntypedCopierWrapper<T> : IDeepCopier<T>
/// <summary>
/// Object pool for <see cref="CopyContext"/> instances.
/// </summary>
public sealed class CopyContextPool
public sealed class CopyContextPool
{
private readonly ConcurrentObjectPool<CopyContext, PoolPolicy> _pool;

Expand Down
Loading