Skip to content

Commit

Permalink
Merge pull request #235 from WeihanLi/dev
Browse files Browse the repository at this point in the history
1.0.73
  • Loading branch information
WeihanLi authored Dec 15, 2024
2 parents 5a802ae + e755f39 commit 5145b55
Show file tree
Hide file tree
Showing 53 changed files with 600 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<Import Project="./build/version.props" />
<PropertyGroup>
<LangVersion>latest</LangVersion>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>WeihanLi</Authors>
Expand Down
23 changes: 12 additions & 11 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@
<PropertyGroup>
<!-- Enable central package management -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0'">6.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">7.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">8.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0</ExtensionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="$(ExtensionPackageVersion)" />
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'net8.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="9.0.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="System.Reflection.Emit" Version="4.7.0" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Serilog" Version="4.1.0" />
<PackageVersion Include="Serilog" Version="4.2.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="FluentAssertions" Version="6.6.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="xunit" Version="2.9.2" />
Expand All @@ -35,10 +36,10 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.9.0" />
<PackageVersion Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageVersion Include="Dapper" Version="2.1.44" />
</ItemGroup>
<ItemGroup>
<GlobalPackageReference Include="PolySharp" Version="1.14.1" />
<GlobalPackageReference Include="PolySharp" Version="1.15.0" />
</ItemGroup>
</Project>
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@
## Release Notes

See pull requests list for changes <https://github.com/WeihanLi/WeihanLi.Common/pulls?q=is%3Apr+is%3Amerged+base%3Amaster>

## Contact

Contact me if you need: <[email protected]>
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>72</VersionPatch>
<VersionPatch>73</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"rollForward": "latestMajor",
"version": "8.0.100",
"version": "9.0.101",
"allowPrerelease": true
}
}
2 changes: 1 addition & 1 deletion samples/DotNetCoreSample/DataExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using WeihanLi.Common;
using WeihanLi.Common.Helpers;
using WeihanLi.Common.Logging;
Expand Down
2 changes: 1 addition & 1 deletion samples/DotNetCoreSample/DotNetCoreSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" />
<PackageReference Include="Microsoft.Extensions.ObjectPool" />
<PackageReference Include="Serilog.Sinks.Console" />
<PackageReference Include="System.Data.SqlClient" />
<PackageReference Include="Microsoft.Data.SqlClient" />
</ItemGroup>

<ItemGroup>
Expand Down
44 changes: 44 additions & 0 deletions samples/DotNetCoreSample/InMemoryStreamTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Weihan Li. All rights reserved.
// Licensed under the Apache license.

using WeihanLi.Common.Helpers;
using WeihanLi.Extensions;

namespace DotNetCoreSample;
internal static class InMemoryStreamTest
{
public static async Task MainTest()
{
var stream = new InMemoryStream<long>("test");
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
await Task.Delay(100);
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
{
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
});
Console.WriteLine("stream message added");
await Task.Delay(1000);
await stream.AddAsync(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), new Dictionary<string, string>
{
{ "messages", new { name = $"test-{DateTimeOffset.Now}" } .ToJson() }
});
Console.WriteLine("stream message added");
//
{
Console.WriteLine("Fetch messages from stream");
await foreach (var item in stream.FetchAsync(timestamp, 2))
{
Console.WriteLine($"{item.Id} - {item.Timestamp}");
Console.WriteLine(item.Fields.ToJson());
}
}
{
Console.WriteLine("Fetch messages from stream again");
await foreach (var item in stream.FetchAsync(timestamp, 2))
{
Console.WriteLine($"{item.Id} - {item.Timestamp}");
Console.WriteLine(item.Fields.ToJson());
}
}
}
}
7 changes: 5 additions & 2 deletions samples/DotNetCoreSample/LoggerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public static void MainTest()
public static void MicrosoftLoggingTest()
{
var services = new ServiceCollection()
.AddLogging(builder => builder.AddConsole())
.AddLogging(builder =>
// builder.AddConsole()
builder.AddFile()
)
.AddSingleton(typeof(GenericTest<>))
.BuildServiceProvider();
services.GetRequiredService<GenericTest<int>>()
Expand All @@ -46,7 +49,7 @@ public static void MicrosoftLoggingTest()
Console.WriteLine();

services = new ServiceCollection()
.AddLogging(builder => builder.AddConsole().UseCustomGenericLogger(options => options.FullNamePredict = _ => true))
.AddLogging(builder => builder.AddConsole().UseCustomGenericLogger())
.AddSingleton(typeof(GenericTest<>))
.BuildServiceProvider();
services.GetRequiredService<GenericTest<int>>()
Expand Down
3 changes: 2 additions & 1 deletion samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@

// InvokeHelper.TryInvoke(() => throw null, 3);

await InvokeHelper.TryInvokeAsync(TemplatingSample.MainTest);
// InvokeHelper.TryInvoke(LoggerTest.MicrosoftLoggingTest);
await InvokeHelper.TryInvokeAsync(InMemoryStreamTest.MainTest);

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");

Expand Down
2 changes: 1 addition & 1 deletion samples/DotNetCoreSample/RepositoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.ObjectPool;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Data.SqlClient;
using WeihanLi.Common;
using WeihanLi.Common.Data;
using WeihanLi.Extensions;
Expand Down
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<PackageIcon>icon.jpg</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>Preview</LangVersion>
<LangVersion>preview</LangVersion>
<NoWarn>$(NoWarn);CS9216;</NoWarn>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Object" Alias="Lock" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" />
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common.Logging.Serilog/SerilogLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public bool IsEnabled(LogLevel logLevel)
return _logger.IsEnabled(ConvertLevel(logLevel));
}

public IDisposable BeginScope<TState>(TState state)
public IDisposable? BeginScope<TState>(TState state) where TState : notnull
{
return _provider.BeginScope(state);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0;net9.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<PackageProjectUrl>https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common.Logging.Serilog</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>$(PackageTags);logging;serilog</PackageTags>
Expand Down
6 changes: 3 additions & 3 deletions src/WeihanLi.Common/Aspect/InternalAspectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ internal static class MethodInvokeHelper
{
public static readonly MethodInfo GetInvocationReturnValueMethod =
typeof(AspectInvocation).GetProperty("ReturnValue")!.GetGetMethod()!;

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly MethodInfo InvokeAspectDelegateMethod =
typeof(AspectDelegate).GetMethod(nameof(AspectDelegate.Invoke))!;

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly MethodInfo GetCurrentMethod =
typeof(MethodBase).GetMethod(nameof(MethodBase.GetCurrentMethod))!;

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly ConstructorInfo AspectInvocationConstructor =
typeof(AspectInvocation).GetConstructors()[0];
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Event/EventBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected EventBase(string eventId)

// https://www.newtonsoft.com/json/help/html/JsonConstructorAttribute.htm
[JsonConstructor]
#if NET6_0_OR_GREATER
#if NET
[System.Text.Json.Serialization.JsonConstructor]
#endif
protected EventBase(string eventId, DateTimeOffset eventAt)
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/CollectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public static IEnumerable<T> GetRandomList<T>(this IList<T> list)
;
}

#if NET6_0_OR_GREATER
#if NET
// https://github.com/more-itertools/more-itertools/blob/master/more_itertools/more.py#L3149
//def set_partitions_helper(L, k):
//n = len(L)
Expand Down
4 changes: 2 additions & 2 deletions src/WeihanLi.Common/Extensions/CoreExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static string ToBase64String(this byte[] inArray, int offset, int length,
return Convert.ToBase64String(inArray, offset, length, options);
}

#if NET5_0_OR_GREATER
#if NET
public static string ToHexString(this ReadOnlySpan<byte> bytes, bool isLowerCase = false)
{
#if NET9_0_OR_GREATER
Expand Down Expand Up @@ -1616,7 +1616,7 @@ public static byte[] HexStringToBytes(this string hexString)
if (string.IsNullOrEmpty(hexString))
return [];

#if NET6_0_OR_GREATER
#if NET
return Convert.FromHexString(hexString);
#else
var charArray = hexString.ToCharArray();
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/DataExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ private static string GetParameterName(string originName)
[typeof(TimeSpan)] = DbType.Time,
[typeof(byte[])] = DbType.Binary,
[typeof(object)] = DbType.Object,
#if NET6_0_OR_GREATER
#if NET
[typeof(DateOnly)] = DbType.Date,
[typeof(TimeOnly)] = DbType.Time,
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/WeihanLi.Common/Extensions/HttpClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static async Task<TResponse?> ReadJsonResponseAsync<TResponse>
{
Guard.NotNull(response);
responseAction?.Invoke(response);
#if NET6_0_OR_GREATER
#if NET
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
#else
var responseText = await response.Content.ReadAsStringAsync();
Expand All @@ -109,15 +109,15 @@ public static async Task<TResponse?> ReadJsonResponseAsync<TResponse>
requestAction?.Invoke(requestMessage);
using var response = await httpClient.SendAsync(requestMessage, cancellationToken);
responseAction?.Invoke(response);
#if NET6_0_OR_GREATER
#if NET
var responseText = await response.Content.ReadAsStringAsync(cancellationToken);
#else
var responseText = await response.Content.ReadAsStringAsync();
#endif
return JsonConvert.DeserializeObject<TResponse>(responseText);
}

#if NET6_0_OR_GREATER
#if NET
/// <summary>
/// PatchAsJsonAsync
/// </summary>
Expand Down
16 changes: 12 additions & 4 deletions src/WeihanLi.Common/Extensions/JsonSerializeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public static class JsonSerializeExtension
/// <summary>
/// DefaultSerializerSettings
/// </summary>
private static readonly JsonSerializerSettings DefaultSerializerSettings = GetDefaultSerializerSettings();
internal static readonly JsonSerializerSettings DefaultSerializerSettings = GetDefaultSerializerSettings();
internal static readonly JsonSerializerSettings WriteIndentedSerializerSettings =
SerializerSettingsWith(s => s.Formatting = Formatting.Indented);

private static JsonSerializerSettings GetDefaultSerializerSettings() => new()
{
Expand Down Expand Up @@ -46,13 +48,19 @@ public static string ToJson(this object? obj)
public static string ToJson(this object obj, JsonSerializerSettings? serializerSettings)
=> obj.ToJson(false, serializerSettings);

/// <summary>
/// Serialize to Indented Json
/// </summary>
/// <param name="obj">object</param>
/// <returns>To Indented Json</returns>
public static string ToIndentedJson(this object obj) => obj.ToJson(false, WriteIndentedSerializerSettings);

/// <summary>
/// 将object对象转换为Json数据
/// </summary>
/// <param name="obj">目标对象</param>
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
public static string ToJson(this object? obj, bool isConvertToSingleQuotes)
=> obj.ToJson(isConvertToSingleQuotes, null);
public static string ToJson(this object? obj, bool isConvertToSingleQuotes) => obj.ToJson(isConvertToSingleQuotes, null);

/// <summary>
/// 将object对象转换为Json数据
Expand All @@ -62,7 +70,7 @@ public static string ToJson(this object? obj, bool isConvertToSingleQuotes)
/// <param name="settings">serializerSettings</param>
public static string ToJson(this object? obj, bool isConvertToSingleQuotes, JsonSerializerSettings? settings)
{
if (obj == null)
if (obj is null)
{
return string.Empty;
}
Expand Down
9 changes: 4 additions & 5 deletions src/WeihanLi.Common/Extensions/ProcessExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public static ProcessStartInfo WithEnv(this ProcessStartInfo processStartInfo, s
return processStartInfo;
}

#if NET6_0_OR_GREATER
#else
#if NETSTANDARD
public static Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
{
Guard.NotNull(process);
Expand Down Expand Up @@ -121,12 +120,12 @@ public static CommandResult GetResult(this ProcessStartInfo psi)
public static async Task<CommandResult> GetResultAsync(this ProcessStartInfo psi, CancellationToken cancellationToken = default)
{
var stdOutStringBuilder = new StringBuilder();
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NET
await
#endif
using var stdOut = new StringWriter(stdOutStringBuilder);
var stdErrStringBuilder = new StringBuilder();
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
#if NET
await
#endif
using var stdErr = new StringWriter(stdErrStringBuilder);
Expand Down Expand Up @@ -285,7 +284,7 @@ public static async Task<Process> ExecuteProcessAsync(this ProcessStartInfo psi,
public static bool TryKill(this Process process, bool entireProcessTree = true)
{
return
#if NET6_0_OR_GREATER
#if NET
process.Try(x => x.Kill(entireProcessTree))
#else
process.Try(x => x.Kill())
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common/Extensions/TaskExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Task AsTask(this CancellationToken cancellationToken)

public static Task WhenAllSafely(this IEnumerable<Task> tasks, Action<Exception>? onException = null) => Task.WhenAll(tasks.Select(async t =>
{
#if NET8_0_OR_GREATER
#if NET
await t.ConfigureAwait(ConfigureAwaitOptions.SuppressThrowing);
#else
try
Expand Down
Loading

0 comments on commit 5145b55

Please sign in to comment.