2023年11月15日,对.net的开发圈是一个重大的日子,.net 8.0正式版发布。

圈内已经预热了有半个月有余,性能不断超越,开发体验越来越完美,早在.net 5.0的时候就各种吹风Aot编译,直到6.0 7.0使用仍然比较麻烦,我个人比较期待本次更新的aot体验。

有的群友几个小时都等不了啦,半夜就开始更新预览版,我是等到第二天早上正式布发布才开始的,开机第一件事情下载.net8.0 SDK,随后更新vs2022企业版。

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP


支持开源

我是开源人:https://github.com/2881099

本文通过我们的开源项目升级,以及AOT试验,记录了整个经验过程。

使用我们开源项目的朋友一般都知道,特点依赖较少(甚至零依赖),每次 .net 新版本发布很轻松就支持了,并且为 AOT 埋下了种子。

第一个要更新的开源项目是FreeRedis,这个项目没有任何外部依赖,本身是支持.net 8.0的,本次维护主要把和测试有关项目类型修改成.net8.0,前后只花了大概十分钟,跑完测试后发布了 FreeRedis 1.2.5

第二个要更新的开源项目是CSRedisCore,大致步骤同上,目前这个项目处于稳定维护阶段,不再增加新功能。

第三个要更新的开源项目是FreeSql,这个项目比较庞大,解决方案内有50个子项目,由于主项目也是零依赖,所以基本不需要修改就支持.net8.0。最新编译器提示.netcoreapp2.1高风险漏洞的警告,不得已移除了.netcoreapp2.1有关的依赖注入支持,前后大约花了半个小时,测试后发布了 FreeSql 3.2.805

第四个要更新的项目是FreeScheduler,这是一个纯净版的定时任务框架,依赖较少只花了5分钟测试发布。

其他几个开源项目稳定且不依赖 .net 版本,所以本次无需维护更新。


测试与支持 FreeRedis aot

下午没事去买了一杯咖啡,到12点钟还睡不着,刷视频刷到一点半还是睡不着,于是想折腾点什么东西,正好.net 8.0 aot特性,测试一下FreeRedis,看看是否支持。

我是直接创建控制台程序测试的,设置成aot发布之后,.csproj 内容如下:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
		<PublishAot>True</PublishAot>
	</PropertyGroup>
	
	<ItemGroup>
		<PackageReference Include="FreeRedis" Version="1.2.5" />
	</ItemGroup>
</Project>

发布 aot 需要使用控制台命令:

第一次发布失败,提示要安装桌面版C++,于是我重新去官网下载vS2022企业版安装器,运行它点击修改安装,选中桌面版C++进行安装,大概过了15分钟,安装完毕。

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

E:\github\FreeRedis\examples\console_net8>dotnet publish -r win-x64 -c release
适用于 .NET MSBuild 版本 17.8.3+195e7f5a3
  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
  console_net8 -> E:\github\FreeRedis\examples\console_net8\bin\release\net8.0\win-x64\console_net8.dll
  Generating native code
C:\Users\28810\.nuget\packages\freeredis\1.2.5\lib\netstandard2.0\FreeRedis.dll : warning IL3053: Assembly 'FreeRedis'
produced AOT analysis warnings. [E:\github\FreeRedis\examples\console_net8\console_net8.csproj]
C:\Users\28810\.nuget\packages\freeredis\1.2.5\lib\netstandard2.0\FreeRedis.dll : warning IL2104: Assembly 'FreeRedis'
produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [E:\github\FreeRedis\examples\c
onsole_net8\console_net8.csproj]
  console_net8 -> E:\github\FreeRedis\examples\console_net8\bin\release\net8.0\win-x64\publish\

只要编译成功,发布aot必然会成功,只是会有一些警告。

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

2023/11/16  13:06         5,637,120 console_net8.exe
2023/11/16  13:06       137,695,232 console_net8.pdb
2023/11/16  04:13           127,268 FreeRedis.pdb

请无视 .pdb 文件,它是调试用途的可以删除,console_net8.exe 只有 5兆大小。

第二次发布后,运行成功了,纯字符串数值之内的操作全部成功。

正当得意之时,redis.AclGetUser 方法抛出了一个新的错误,该方法返回的是一个实体类型 AclGetUserResult,有使用 Activetor.CreateInstance(typeof(AclGetUserResult)),Aot本身是支持这个方法的,错误提示是不支持该方法的对象类型 AclUserResult。

Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'FreeRedis.AclGetUserResult'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x119
   at FreeRedis.RespHelper.CreateInstanceGetDefaultValue(Type) + 0x120
   at FreeRedis.RespHelper.MapToClass[T](Object[], Encoding) + 0x4a
   at FreeRedis.RedisClient.<>c__DisplayClass457_0.<AclGetUser>b__1(Object[] a, Boolean _) + 0x220
   at FreeRedis.RedisResult.ThrowOrValue[TValue](Func`3) + 0x58
   at FreeRedis.RedisClient.PoolingAdapter.<>c__DisplayClass9_0`1.<AdapterCall>b__0() + 0x141
   at FreeRedis.RedisClient.LogCallCtrl[T](CommandPacket cmd, Func`1 func, Boolean aopBefore, Boolean aopAfter) + 0x3bb
   at FreeRedis.RedisClient.LogCall[T](CommandPacket cmd, Func`1 func) + 0x63
   at FreeRedis.RedisClient.PoolingAdapter.AdapterCall[TValue](CommandPacket, Func`2) + 0x9a
   at console_net8.Program.Main(String[] args) + 0xa4
   at console_net8!<BaseAddress>+0x2c67f0

于是我系统的去看了官方aot文档,发现文档太过于简陋,反复看了七八遍也没有找到相关的解决内容。不得已扩大了搜索范围,在谷歌搜索关键字 .net aot 花了近一个小时,最终定位的关键字是 rd.xml,配置相当简单,只需要把FreeRedis的类型全部配置即可。

对应的 .csproj 内容如下:

<Project Sdk="Microsoft.NET.Sdk">
	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net8.0</TargetFramework>
		<PublishAot>True</PublishAot>
	</PropertyGroup>

	<ItemGroup>
		<RdXmlFile Include="rd.xml" />
	</ItemGroup>

	<ItemGroup>
		<PackageReference Include="FreeRedis" Version="1.2.5" />
	</ItemGroup>
</Project>

对应的 rd.xml 内容如下:

<Directives>
	<Application>
		<Assembly Name="FreeRedis"  Dynamic="Required All">
		</Assembly>
	</Application>
</Directives>

重新发布后,完美的解决所有问题,.exe 文件体积增涨到 7兆。

这多亏当初设计FreeRedis的时候把依赖简单最低,才这么容易支持更多的运行平台。


第二轮aot试验 FreeScheduler

FreeRedis对.net 8.0以及aot的支持完美收官,这个时候已经凌晨三点,咖啡的劲还很足。

我本身对FreeSql Aot是不抱希望的,所以,就去测试FreeScheduler了。

FreeScheduler支持三种存储方式,内存/数据库/redis

基于内存,毫无压力,直接通过测试。(得益于依赖较少)

基于redis,由于FreeRdis通过了aot测试,基本不会有太大的问题,记得设置好rd.xml,顺利通过。

对应的 .csproj 如下:

<Project Sdk="Microsoft.NET.Sdk.Web">
	<PropertyGroup>
		<TargetFramework>net8.0</TargetFramework>
		<Nullable>enable</Nullable>
		<ImplicitUsings>enable</ImplicitUsings>
		<InvariantGlobalization>true</InvariantGlobalization>
		<PublishAot>true</PublishAot>
	</PropertyGroup>

	<ItemGroup>
		<RdXmlFile Include="rd.xml" />
	</ItemGroup>

	<ItemGroup>
		<ProjectReference Include="..\..\FreeScheduler\FreeScheduler.csproj" />
	</ItemGroup>
</Project>

对应的 rd.xml 内容如下:

<Directives>
	<Application>
		<Assembly Name="FreeScheduler"  Dynamic="Required All">
		</Assembly>
		<Assembly Name="FreeRedis"  Dynamic="Required All">
		</Assembly>
	</Application>
</Directives>

FreeScheduler还有一个web管理面板功能,抱着尝试的态度试一试,创建.net8.0自带的web API aot项目,把有关代码加到项目的运行,居然能直接通过,太牛逼了,这是我没有想到的。

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

对应 Program.cs

using FreeRedis;
using FreeScheduler;
using Newtonsoft.Json;

var redis = new RedisClient("127.0.0.1,poolsize=10,exitAutoDisposePool=false");
redis.Serialize = obj => JsonConvert.SerializeObject(obj);
redis.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
redis.Notice += (s, e) =>
{
    if (e.Exception != null)
        Console.WriteLine(e.Log);
};
Scheduler scheduler = new FreeSchedulerBuilder()
    .OnExecuting(task =>
    {
        Console.WriteLine($"[{DateTime.Now.ToString("HH:mm:ss.fff")}] {task.Topic} 被执行");
        task.Remark("log..");
    })
    .UseStorage(redis)
    .Build();
if (Datafeed.GetPage(scheduler, null, null, null, null).Total == 0)
{
    scheduler.AddTask("[系统预留]清理任务数据", "86400", -1, 3600);
    scheduler.AddTaskRunOnWeek("(周一)武林大会", "json", -1, "1:12:00:00");
    scheduler.AddTaskRunOnWeek("(周日)亲子活动", "json", -1, "0:00:00:00");
    scheduler.AddTaskRunOnWeek("(周六)社交活动", "json", -1, "6:00:00:00");
    scheduler.AddTaskRunOnMonth("月尾最后一天", "json", -1, "-1:16:00:00");
    scheduler.AddTaskRunOnMonth("月初第一天", "json", -1, "1:00:00:00");
    scheduler.AddTask("定时20秒", "json", 10, 20);
    scheduler.AddTask("测试任务1", "json", new[] { 10, 30, 60, 100, 150, 200 });
}

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.AddSingleton(scheduler);

var app = builder.Build();
var applicationLifeTime = app.Services.GetService<IHostApplicationLifetime>();
applicationLifeTime.ApplicationStopping.Register(() =>
{
    scheduler.Dispose();
    redis.Dispose();
});
app.UseFreeSchedulerUI("/freescheduler/");

app.Run();

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

2023/11/16  04:23               127 appsettings.Development.json
2023/11/16  04:23               151 appsettings.json
2023/11/16  13:34        25,104,384 Examples_FreeScheduler_Net80_aot.exe
2023/11/16  13:34       238,948,352 Examples_FreeScheduler_Net80_aot.pdb
2023/11/16  13:31            31,208 FreeScheduler.pdb

Examples_FreeScheduler_Net80_aot.exe 25兆,流弊了,双击运行它吧~~~~

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

打开浏览器访问:http://localhost:5000/freescheduler/

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

意想不到,连管理面板都支持 AOT,这让我有了继续试验的动力~~~


aot试验意外收获 FreeSql

四点了,还没犯困!

最后抱着必凉的心态尝试终极试验,FreeScheduler使用数据库持久化。

第一次失败,报错在FreeSql内部,这是是有TaskInterval类型不存在,它其实是FreeScheduler程序集的,并且rd.xml已经配置好了,反复折腾仍然报错。

Unhandled Exception: System.NotSupportedException: 'FreeScheduler.TaskInterval[]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.General.TypeUnifier.WithVerifiedTypeHandle(RuntimeArrayTypeInfo, RuntimeTypeInfo) + 0x54
   at System.Array.InternalCreate(RuntimeType elementType, Int32 rank, Int32* pLengths, Int32* pLowerBounds) + 0x64
   at System.Array.CreateInstance(Type elementType, Int32 length) + 0x46
   at System.RuntimeType.GetEnumValues() + 0x53
   at FreeSql.Internal.Utils.GetTableByEntity(Type entity, CommonUtils common) + 0x138a
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.<SyncStructure>b__51_0(CodeFirstProvider.TypeAndName a) + 0x6e
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() + 0x3f
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray() + 0x5c
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure(CodeFirstProvider.TypeAndName[] objects) + 0xd6
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure[TEntity]() + 0x6c
   at FreeScheduler.TaskHandlers.FreeSqlHandler..ctor(IFreeSql fsql) + 0xf0
   at FreeSchedulerBuilder.Build() + 0x2f
   at Program.<Main>$(String[] args) + 0x180
   at Examples_FreeScheduler_Net80_aot!<BaseAddress>+0xca7fc3

从堆栈 GetEnumValues 可以看出是执行 Enum.GetValues 报错,通过不断尝试中的其中一次,在程序开始写了一行:

Console.WriteLine(Enum.GetValues(typeof(TaskInterval)));

重新发布后,又出现另一个错误,大致与上面的相同,只是类型变成了 TaskStatus,同样加上一行代码:

Console.WriteLine(Enum.GetValues(typeof(TaskStatus)));

又出现了另一个错误:

Unhandled Exception: System.InvalidOperationException: The binary operator Equal is not defined for the types 'System.Reflection.Runtime.TypeInfos.NativeFormat.NativeFormatRuntimeNamedTypeInfo' and 'System.Reflection.Runtime.TypeInfos.RuntimeConstructedGenericTypeInfo'.
   at System.Linq.Expressions.Expression.GetEqualityComparisonOperator(ExpressionType, String, Expression, Expression, Boolean) + 0x26b
   at System.Linq.Expressions.Expression.Equal(Expression, Expression, Boolean, MethodInfo) + 0x63
   at FreeSql.Internal.Utils.<GetDataReaderValueBlockExpression>g__LocalFuncGetExpression|65_0(Boolean ignoreArray, Utils.<>c__DisplayClass65_0&) + 0x3916
   at FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(Type type, Expression value) + 0x18d
   at FreeSql.Internal.Utils.<>c__DisplayClass66_0.<GetDataReaderValue>b__1(Type valueType2) + 0x68
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at FreeSql.Internal.Utils.GetDataReaderValue(Type type, Object value) + 0x147
   at FreeSql.Internal.Utils.GetTableByEntity(Type entity, CommonUtils common) + 0x145c
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.<SyncStructure>b__51_0(CodeFirstProvider.TypeAndName a) + 0x6e
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() + 0x3f
   at System.Linq.Enumerable.WhereEnumerableIterator`1.ToArray() + 0x5c
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure(CodeFirstProvider.TypeAndName[] objects) + 0xd6
   at FreeSql.Internal.CommonProvider.CodeFirstProvider.SyncStructure[TEntity]() + 0x6c
   at FreeScheduler.TaskHandlers.FreeSqlHandler..ctor(IFreeSql fsql) + 0xf0
   at FreeSchedulerBuilder.Build() + 0x2f
   at Program.<Main>$(String[] args) + 0x1a8
   at Examples_FreeScheduler_Net80_aot!<BaseAddress>+0xca8423

这次使用 vs2022 附加进程的方式进行了调试,深入 FreeSql 内部源码(表达式树)环境,定位到了一行代码:

Expression.Equal(type, Expression.Contrast(Guid?))

把 Guid? 后面的问题去掉后,再次发布。(由于每次发布时间要20-30秒,重试时间成本太高,卡在这个问题已经有半个小时)

2023/11/16  04:23               127 appsettings.Development.json
2023/11/16  04:23               151 appsettings.json
2023/11/16  13:52        29,018,112 Examples_FreeScheduler_Net80_aot.exe
2023/11/16  13:52       238,948,352 Examples_FreeScheduler_Net80_aot.pdb
2023/11/16  13:31            31,208 FreeScheduler.pdb
2021/11/03  01:47         1,763,632 SQLite.Interop.dll

对于一个 web 项目并且包含 bootstrap 有关静态资源文件,.exe 文件只有 29兆太满意了

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP

看到控制台上的 SQL,太惊喜了,成功啦~~~~

.NET8.0 AOT 经验分享 FreeSql/FreeRedis/FreeScheduler 均已通过测试-LMLPHP


最后建议

从 .net6.0 到 .net8.0,我们肉眼看不到变化,实际微软做了很多内部工作,在 aot 使用体验上明显能感知。

有人说信创国产运行,那现在 aot 算什么?

.net8.0 AOT 已经到了可用的阶段,期待未来版本能改进以下问题:

  • 发布速度变快,目前20-30秒一次实在太慢
  • 编译前检查错误,而不是等发布后再报运行时错误
  • 加强调试,.pdb 100兆++ 为何调试还都是 c++ 有关内容,不能白瞎了这么大的调试文件啊
  • 尽快修复 Console.WriteLine(Enum.GetValues(typeof(TaskInterval))) 这个问题

我是开源人:https://github.com/2881099

Native AOT apps have the following limitations:

  • No dynamic loading, for example, Assembly.LoadFile.
  • No run-time code generation, for example, System.Reflection.Emit.
  • No C++/CLI.
  • Windows: No built-in COM.
  • Requires trimming, which has limitations.
  • Implies compilation into a single file, which has known incompatibilities.
  • Apps include required runtime libraries (just like self-contained apps, increasing their size as compared to framework-dependent apps).
  • System.Linq.Expressions always use their interpreted form, which is slower than run-time generated compiled code.
  • Not all the runtime libraries are fully annotated to be Native AOT compatible. That is, some warnings in the runtime libraries aren't actionable by end developers.
11-17 11:48