Skip to content

Commit

Permalink
feat: Updated to NET9 and added trimming/NativeAOT support.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Dec 6, 2024
1 parent 607003e commit 1f2edcb
Show file tree
Hide file tree
Showing 32 changed files with 226 additions and 47 deletions.
9 changes: 9 additions & 0 deletions H.Pipes.sln
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Formatters.Inferno", "src
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "H.Pipes.Apps.ConsoleApp.Inferno", "src\samples\H.Pipes.Apps.ConsoleApp.Inferno\H.Pipes.Apps.ConsoleApp.Inferno.csproj", "{221023C5-A9D4-4C03-9E85-32518F1D7850}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "helpers", "helpers", "{D7968FE4-8E81-4877-999B-9DBDA9C354C4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimmingHelper", "src\helpers\TrimmingHelper\TrimmingHelper.csproj", "{DE736C37-E059-45B8-B58D-74BE01413D7F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -125,6 +129,10 @@ Global
{221023C5-A9D4-4C03-9E85-32518F1D7850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{221023C5-A9D4-4C03-9E85-32518F1D7850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{221023C5-A9D4-4C03-9E85-32518F1D7850}.Release|Any CPU.Build.0 = Release|Any CPU
{DE736C37-E059-45B8-B58D-74BE01413D7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE736C37-E059-45B8-B58D-74BE01413D7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE736C37-E059-45B8-B58D-74BE01413D7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE736C37-E059-45B8-B58D-74BE01413D7F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -145,6 +153,7 @@ Global
{FDECF262-8536-4488-8DBE-8B9961F0C991} = {A30A9D7F-0824-48AD-96F9-2944421541AD}
{C631662C-2FC4-4C77-93A2-385B06436EA6} = {DFAEA034-20D3-46BD-A2FB-E09CB731C4D7}
{221023C5-A9D4-4C03-9E85-32518F1D7850} = {A30A9D7F-0824-48AD-96F9-2944421541AD}
{DE736C37-E059-45B8-B58D-74BE01413D7F} = {D7968FE4-8E81-4877-999B-9DBDA9C354C4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3A24ECAC-930F-4D6F-B45E-07B16DD51C4E}
Expand Down
1 change: 1 addition & 0 deletions src/helpers/TrimmingHelper/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Console.WriteLine("Build, rebuild or publish this app to see trimming warnings.");
36 changes: 36 additions & 0 deletions src/helpers/TrimmingHelper/TrimmingHelper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>

<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\libs\H.Formatters.System.Text.Json\H.Formatters.System.Text.Json.csproj" />
<ProjectReference Include="..\..\libs\H.Formatters\H.Formatters.csproj" />
<ProjectReference Include="..\..\libs\H.Pipes.AccessControl\H.Pipes.AccessControl.csproj" />
<ProjectReference Include="..\..\libs\H.Pipes\H.Pipes.csproj" />
</ItemGroup>

<ItemGroup>
<TrimmerRootAssembly Include="H.Formatters" />
<TrimmerRootAssembly Include="H.Formatters.System.Text.Json" />
<TrimmerRootAssembly Include="H.Pipes" />
<TrimmerRootAssembly Include="H.Pipes.AccessControl" />
</ItemGroup>

<PropertyGroup Label="Publish">
<RuntimeIdentifier Condition="$([MSBuild]::IsOSPlatform('windows'))">win-x64</RuntimeIdentifier>
<RuntimeIdentifier Condition="!$([MSBuild]::IsOSPlatform('windows'))">osx-arm64</RuntimeIdentifier>

<SelfContained>true</SelfContained>
</PropertyGroup>

<Target Name="ProduceTrimmingWarnings" AfterTargets="Build">
<CallTarget Targets="Publish"/>
</Target>

</Project>
8 changes: 8 additions & 0 deletions src/libs/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,12 @@
<AnalysisMode>All</AnalysisMode>
</PropertyGroup>

<PropertyGroup Label="Trimmable" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsAotCompatible>true</IsAotCompatible>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
<IsTrimmable>true</IsTrimmable>
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>

</Project>
4 changes: 4 additions & 0 deletions src/libs/H.Formatters.BinaryFormatter/BinaryFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
/// <summary>
/// A formatter that uses <see cref="System.Runtime.Serialization.Formatters.Binary.BinaryFormatter"/> inside for serialization/deserialization
/// </summary>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("BinaryFormatter serialization uses dynamic code generation, the type of objects being processed cannot be statically discovered.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("BinaryFormatter serialization is not trim compatible because the type of objects being processed cannot be statically discovered.")]
#endif
public class BinaryFormatter : FormatterBase
{
public System.Runtime.Serialization.Formatters.Binary.BinaryFormatter InternalFormatter { get; } = new();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CA2300;CA2302;CS1591</NoWarn>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/libs/H.Formatters.Ceras/H.Formatters.Ceras.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.5.1;net4.7.2;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.5.1;net4.7.2;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CS1591</NoWarn>
<SignAssembly>false</SignAssembly>
Expand Down
2 changes: 1 addition & 1 deletion src/libs/H.Formatters.Inferno/H.Formatters.Inferno.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.6.2;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.6.2;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CA1031;CA1819;CS1591</NoWarn>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
Expand Down
3 changes: 2 additions & 1 deletion src/libs/H.Formatters.Inferno/PipeClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static class PipeClientExtensions
/// <param name="exceptionAction"></param>
/// <exception cref="ArgumentNullException"></exception>
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
public static void EnableEncryption<T>(
this IPipeClient<T> client,
Action<Exception>? exceptionAction = null)
Expand All @@ -31,7 +32,7 @@ public static void EnableEncryption<T>(
using var source = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var cancellationToken = source.Token;

var client = new SingleConnectionPipeClient<byte[]>(pipeName, args.Connection.ServerName, formatter: args.Connection.Formatter);
var client = new SingleConnectionPipeClient<byte[]>(pipeName, formatter: args.Connection.Formatter, args.Connection.ServerName);
client.ExceptionOccurred += (_, args) =>
{
Debug.WriteLine($"{nameof(EnableEncryption)} client returns exception: {args.Exception}");
Expand Down
1 change: 1 addition & 0 deletions src/libs/H.Formatters.Inferno/PipeServerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public static class PipeServerExtensions
/// <param name="pipeSecurity"></param>
/// <exception cref="ArgumentNullException"></exception>
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
public static void EnableEncryption<T>(
this IPipeServer<T> server,
Action<Exception>? exceptionAction = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.6.2;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.6.2;net8.0;net9.0</TargetFrameworks>
<RootNamespace>H.Formatters</RootNamespace>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
Expand All @@ -15,11 +15,11 @@
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net4.6.2' ">
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ namespace H.Formatters;
/// <summary>
/// A formatter that uses <see cref="JsonSerializer"/> inside for serialization/deserialization
/// </summary>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
#endif
public class SystemTextJsonFormatter : FormatterBase
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace H.Formatters;

/// <summary>
/// A formatter that uses <see cref="JsonSerializer"/> inside for serialization/deserialization
/// </summary>
public class SystemTextJsonNativeAotFormatter(JsonSerializerContext context) : FormatterBase
{
/// <summary>
/// Default: UTF8.
/// </summary>
public Encoding Encoding { get; set; } = Encoding.UTF8;

/// <summary>
/// Set this to enable trimming/NativeAOT support.
/// </summary>
public JsonSerializerContext Context { get; set; } = context;

protected override byte[] SerializeInternal(object? obj)
{
if (obj == null)
{
return [];
}

var json = JsonSerializer.Serialize(obj, obj.GetType(), Context);
var bytes = Encoding.GetBytes(json);

return bytes;
}

protected override T DeserializeInternal<T>(byte[] bytes)
{
var json = Encoding.GetString(bytes);
var obj = (T?)JsonSerializer.Deserialize(json, typeof(T), Context);

return obj ?? throw new InvalidOperationException("obj is null.");
}
}
2 changes: 1 addition & 1 deletion src/libs/H.Formatters/H.Formatters.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net4.5.1;net8.0;net9.0</TargetFrameworks>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net4.6.2;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net4.6.2;net8.0;net9.0</TargetFrameworks>
<NoWarn>$(NoWarn);CA1003;CA1031;CS3016</NoWarn>
<PolySharpIncludeRuntimeSupportedAttributes>true</PolySharpIncludeRuntimeSupportedAttributes>
</PropertyGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/libs/H.Pipes.AccessControl/PipeApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace H.Pipes.AccessControl;
Description = "Occurs when new exception.")]
[Event<IReadOnlyCollection<string>>("ArgumentsReceived", PropertyNames = new[] { "Arguments" },
Description = "Occurs when new arguments received.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode(
"JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode(
"JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
public sealed partial class PipeApplication : IAsyncDisposable
{
#region Properties
Expand Down
6 changes: 6 additions & 0 deletions src/libs/H.Pipes/Extensions/ConnectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static class ConnectionExtensions
/// <param name="cancellationToken"></param>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<ConnectionMessageEventArgs<T>> WaitMessageAsync<T>(this IPipeConnection<T> connection, Func<CancellationToken, Task>? func = null, CancellationToken cancellationToken = default)
{
return await connection.WaitEventAsync<ConnectionMessageEventArgs<T>>(
Expand All @@ -35,6 +38,9 @@ public static async Task<ConnectionMessageEventArgs<T>> WaitMessageAsync<T>(this
/// <param name="func"></param>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<ConnectionMessageEventArgs<T>> WaitMessageAsync<T>(this IPipeConnection<T> connection, TimeSpan timeout, Func<CancellationToken, Task>? func = null)
{
using var tokenSource = new CancellationTokenSource(timeout);
Expand Down
12 changes: 12 additions & 0 deletions src/libs/H.Pipes/Extensions/EventExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public void HandleEvent(object sender, T e)
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<T> WaitEventAsync<T>(this object value, string eventName, CancellationToken cancellationToken = default)
{
value = value ?? throw new ArgumentNullException(nameof(value));
Expand Down Expand Up @@ -81,6 +84,9 @@ public static async Task<T> WaitEventAsync<T>(this object value, string eventNam
/// <exception cref="InvalidOperationException"></exception>
/// <exception cref="OperationCanceledException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<T> WaitEventAsync<T>(this object value, Func<CancellationToken, Task> func, string eventName, CancellationToken cancellationToken = default)
{
value = value ?? throw new ArgumentNullException(nameof(value));
Expand Down Expand Up @@ -109,6 +115,9 @@ public static async Task<T> WaitEventAsync<T>(this object value, Func<Cancellati
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<Dictionary<string, T>> WaitAllEventsAsync<T>(this object value, Func<CancellationToken, Task> func, CancellationToken cancellationToken = default, params string[] eventNames)
{
value = value ?? throw new ArgumentNullException(nameof(value));
Expand Down Expand Up @@ -163,6 +172,9 @@ public static async Task<Dictionary<string, T>> WaitAllEventsAsync<T>(this objec
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
/// <returns></returns>
#if NET6_0_OR_GREATER
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This method uses reflection and may not work properly with trimming")]
#endif
public static async Task<Dictionary<string, T>> WaitAnyEventAsync<T>(this object value, Func<CancellationToken, Task> func, CancellationToken cancellationToken = default, params string[] eventNames)
{
value = value ?? throw new ArgumentNullException(nameof(value));
Expand Down
10 changes: 5 additions & 5 deletions src/libs/H.Pipes/H.Pipes.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net4.6.2;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netstandard2.1;net4.6.2;net8.0;net9.0</TargetFrameworks>
<NoWarn>$(NoWarn);CA1031;CS3016</NoWarn>
</PropertyGroup>

Expand All @@ -23,8 +23,8 @@
</PropertyGroup>

<ItemGroup Label="Usings">
<Using Include="H.Formatters.BinaryFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' != 'net8.0' " />
<Using Include="H.Formatters.SystemTextJsonFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' == 'net8.0' " />
<Using Include="H.Formatters.BinaryFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' != 'net8.0' AND '$(TargetFramework)' != 'net9.0' " />
<Using Include="H.Formatters.SystemTextJsonFormatter" Alias="DefaultFormatter" Condition=" '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net9.0' " />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
Expand All @@ -35,11 +35,11 @@
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="9.0.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'net8.0' ">
<ItemGroup Condition=" '$(TargetFramework)' != 'net8.0' AND '$(TargetFramework)' != 'net9.0' ">
<ProjectReference Include="..\H.Formatters.BinaryFormatter\H.Formatters.BinaryFormatter.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net9.0' ">
<ProjectReference Include="..\H.Formatters.System.Text.Json\H.Formatters.System.Text.Json.csproj" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 1f2edcb

Please sign in to comment.