Skip to content

Commit e69da15

Browse files
authored
Merge pull request #253 from dotnetcore/aot
Aot
2 parents 923504f + 368c828 commit e69da15

21 files changed

+194
-42
lines changed

App/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ public void ConfigureServices(IServiceCollection services)
4949
services
5050
.AddWebApiClient()
5151
.UseJsonFirstApiActionDescriptor()
52-
.UseSourceGeneratorHttpApiActivator()
53-
.AddDynamicDependencyApp();
52+
.UseSourceGeneratorHttpApiActivator();
5453

5554
// 注册userApi
5655
services.AddHttpApi(typeof(IUserApi), o =>

AppAot/AppAot.csproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
7+
<Nullable>enable</Nullable>
8+
<PublishTrimmed>true</PublishTrimmed>
9+
<PublishAot>true</PublishAot>
10+
<InvariantGlobalization>true</InvariantGlobalization>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\WebApiClientCore.Extensions.SourceGenerator\WebApiClientCore.Extensions.SourceGenerator.csproj" />
19+
<ProjectReference Include="..\WebApiClientCore\WebApiClientCore.csproj" />
20+
<ProjectReference Include="..\WebApiClientCore.Analyzers\WebApiClientCore.Analyzers.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
21+
<ProjectReference Include="..\WebApiClientCore.Analyzers.SourceGenerator\WebApiClientCore.Analyzers.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
22+
</ItemGroup>
23+
24+
</Project>

AppAot/AppData.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AppAot
2+
{
3+
public class AppData
4+
{
5+
public string? WebpackCompilationHash { get; set; }
6+
}
7+
}

AppAot/AppHostedService.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading;
9+
using System.Threading.Tasks;
10+
11+
namespace AppAot
12+
{
13+
class AppHostedService : BackgroundService
14+
{
15+
private readonly IServiceScopeFactory serviceScopeFactory;
16+
private readonly ILogger<AppHostedService> logger;
17+
18+
public AppHostedService(
19+
IServiceScopeFactory serviceScopeFactory,
20+
ILogger<AppHostedService> logger)
21+
{
22+
this.serviceScopeFactory = serviceScopeFactory;
23+
this.logger = logger;
24+
}
25+
26+
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
27+
{
28+
using var scope = this.serviceScopeFactory.CreateScope();
29+
var api = scope.ServiceProvider.GetRequiredService<ICloudflareApi>();
30+
var appData = await api.GetAppDataAsync();
31+
appData = await api.GetAppData2Async();
32+
this.logger.LogInformation($"WebpackCompilationHash: {appData.WebpackCompilationHash}");
33+
}
34+
}
35+
}

AppAot/AppJsonSerializerContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace AppAot
4+
{
5+
[JsonSerializable(typeof(AppData[]))]
6+
partial class AppJsonSerializerContext : JsonSerializerContext
7+
{
8+
}
9+
}

AppAot/ICloudflareApi.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Threading.Tasks;
2+
using WebApiClientCore;
3+
using WebApiClientCore.Attributes;
4+
5+
namespace AppAot
6+
{
7+
[HttpHost("https://www.cloudflare-cn.com")]
8+
[LoggingFilter]
9+
public interface ICloudflareApi
10+
{
11+
[HttpGet("/page-data/app-data.json")]
12+
Task<AppData> GetAppDataAsync();
13+
14+
[HttpGet("/page-data/app-data.json")]
15+
ITask<AppData> GetAppData2Async();
16+
}
17+
}

AppAot/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace AppAot
5+
{
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
Host.CreateDefaultBuilder(args)
11+
.ConfigureServices(services =>
12+
{
13+
services
14+
.AddWebApiClient()
15+
.UseSourceGeneratorHttpApiActivator() // SG 激活器
16+
.ConfigureHttpApi(options => // json SG生成器配置
17+
{
18+
var jsonContext = AppJsonSerializerContext.Default;
19+
options.JsonSerializeOptions.TypeInfoResolverChain.Insert(0, jsonContext);
20+
options.JsonDeserializeOptions.TypeInfoResolverChain.Insert(0, jsonContext);
21+
options.KeyValueSerializeOptions.GetJsonSerializerOptions().TypeInfoResolverChain.Insert(0, jsonContext);
22+
});
23+
24+
services.AddHttpApi<ICloudflareApi>();
25+
services.AddHostedService<AppHostedService>();
26+
})
27+
.Build()
28+
.Run();
29+
}
30+
}
31+
}

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
3-
<Version>2.0.5</Version>
4-
<Copyright>Copyright © laojiu 2017-2023</Copyright>
3+
<Version>2.0.6</Version>
4+
<Copyright>Copyright © laojiu 2017-2024</Copyright>
55
<NoWarn>IDE0057</NoWarn>
66
</PropertyGroup>
77

WebApiClientCore.Abstractions/IHttpApiActivator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
/// 定义THttpApi的实例创建器的接口
55
/// </summary>
66
/// <typeparam name="THttpApi"></typeparam>
7-
public interface IHttpApiActivator<THttpApi>
7+
public interface IHttpApiActivator<
8+
#if NET5_0_OR_GREATER
9+
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
10+
#endif
11+
THttpApi>
812
{
913
/// <summary>
1014
/// 创建THttpApi的代理实例

WebApiClientCore.Abstractions/WebApiClientCore.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Nullable>enable</Nullable>
5-
<TargetFramework>netstandard2.1</TargetFramework>
5+
<TargetFrameworks>netstandard2.1;net5.0</TargetFrameworks>
66

77
<RootNamespace>WebApiClientCore</RootNamespace>
88
<AssemblyName>WebApiClientCore.Abstractions</AssemblyName>

0 commit comments

Comments
 (0)