之前测试失败,应该是虽然运行时是8.0但是平台没有指定8.0导致,
环境检查
C:\src\netcorapibase>dotnet --info
.NET SDK:
Version: 8.0.100
Commit: 57efcf1350
Workload version: 8.0.100-manifests.3b83835e
运行时环境:
OS Name: Windows
OS Version: 10.0.19045
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\8.0.100\
已安装 .NET 工作负载:
Workload version: 8.0.100-manifests.3b83835e
没有要显示的已安装工作负载。
Host:
Version: 8.0.0
Architecture: x64
Commit: 5535e31a71
.NET SDKs installed:
5.0.405 [C:\Program Files\dotnet\sdk]
7.0.104 [C:\Program Files\dotnet\sdk]
7.0.202 [C:\Program Files\dotnet\sdk]
8.0.100 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables:
Not set
global.json file:
Not found
Learn more:
https://aka.ms/dotnet/info
Download .NET:
https://aka.ms/dotnet/download
C:\src\netcorapibase>
项目指定环境,<TargetFramework>net8.0</TargetFramework>
<PropertyGroup>
<PublishAot>true</PublishAot>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<StripSymbols>true</StripSymbols>
<UserSecretsId>37c2da8e-4c70-4b1a-b525-dc02325e9e4c</UserSecretsId>
<OutputType>Exe</OutputType>
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
<Configurations>xxx</Configurations>
</PropertyGroup>
直接不aot,直接调试模式运行
Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property.
解决办法
msbuild配置加上
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
具体参考微软博客提供的信息如下:
https://devblogs.microsoft.com/dotnet/system-text-json-in-dotnet-8/
具体资料链接
也就是说默认就禁用了反射默认值, 把它开启就可以解决。
我aot编译比较喜欢用的方式
dotnet publish 你的项目.csproj -r win-x64 -c CHANNEL
出现
Swashbuckle.AspNetCore.Filters.AddResponseHeadersFilter' could not be located
for type 'Swashbuckle.AspNetCore.Filters.AddResponseHeadersFilter' could not be located. Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments. Also ensure no extraneous arguments are provided.
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.FindApplicableConstructor(Type, Type[], ConstructorInfo&, Nullable`1[]&) + 0xa1
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider, Type, Object[]) + 0x447
https://github.com/dotnet/aspnetcore/issues/53125
这是我的提问,经过大佬的回答,我发现不是我想的那么完美
我visual studio新建项目 发现多了个native aot
和传统的,是做了一些改变,
dotnet publish
直接搞定
也就是说要完美兼容,他的demo就是完美的,没有controller,也就是说controller就是mvc的那套,所以不支持的
他的demo的一些地方就是aot的源生成
而slimbuilder进行了特殊处理,更兼容一些。
参考
https://learn.microsoft.com/zh-cn/aspnet/core/fundamentals/native-aot?view=aspnetcore-8.0#aspnet-core-and-native-aot-compatibility
所以正如我那提问 然后大佬说的,就算碰巧支持,那也是不分支持
不过呢,进行aot后变成单文件,普通人是无法进行逆向了,我认为目的已经达到了
另外没有controller,就要自行研究 怎么封装auth授权等东西了。
jwt目前是有了,但是没有以前那套还是差了点东西,先暂时观望吧