Skip to content

Commit 33506f1

Browse files
committed
完整实现AOT
1 parent 347a627 commit 33506f1

File tree

9 files changed

+40
-14
lines changed

9 files changed

+40
-14
lines changed

AppAot/AppHostedService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
2828
using var scope = this.serviceScopeFactory.CreateScope();
2929
var api = scope.ServiceProvider.GetRequiredService<ICloudflareApi>();
3030
var appData = await api.GetAppDataAsync();
31+
appData = await api.GetAppData2Async();
3132
this.logger.LogInformation($"WebpackCompilationHash: {appData.WebpackCompilationHash}");
3233
}
3334
}

AppAot/ICloudflareApi.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Threading.Tasks;
2+
using WebApiClientCore;
23
using WebApiClientCore.Attributes;
34

45
namespace AppAot
@@ -9,5 +10,8 @@ public interface ICloudflareApi
910
{
1011
[HttpGet("/page-data/app-data.json")]
1112
Task<AppData> GetAppDataAsync();
13+
14+
[HttpGet("/page-data/app-data.json")]
15+
ITask<AppData> GetAppData2Async();
1216
}
1317
}

AppAot/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AppAot
66
class Program
77
{
88
static void Main(string[] args)
9-
{
9+
{
1010
Host.CreateDefaultBuilder(args)
1111
.ConfigureServices(services =>
1212
{

WebApiClientCore.Abstractions/IHttpApiActivator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <typeparam name="THttpApi"></typeparam>
77
public interface IHttpApiActivator<
88
#if NET5_0_OR_GREATER
9-
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
9+
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
1010
#endif
1111
THttpApi>
1212
{

WebApiClientCore.Analyzers.SourceGenerator/DynamicDependencyBuilder.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.CodeAnalysis;
22
using Microsoft.CodeAnalysis.Text;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Text;
56

67
namespace WebApiClientCore.Analyzers.SourceGenerator
@@ -35,7 +36,7 @@ public override string ToString()
3536
builder.AppendLine("#if NET5_0_OR_GREATER");
3637
builder.AppendLine("using System.Diagnostics.CodeAnalysis;");
3738
builder.AppendLine("using System.Runtime.CompilerServices;");
38-
builder.AppendLine($"namespace WebApiClientCore");
39+
builder.AppendLine($"namespace WebApiClientCore.Implementations");
3940
builder.AppendLine("{");
4041
builder.AppendLine(" /// <summary>动态依赖初始化器</summary>");
4142
builder.AppendLine($" static partial class {this.ClassName}");
@@ -47,11 +48,19 @@ public override string ToString()
4748
/// 避免程序集在裁剪时裁剪掉由SourceGenerator生成的代理类
4849
/// </summary>
4950
""");
51+
52+
builder.AppendLine(" [ModuleInitializer]");
5053
foreach (var codeBuilder in this.codeBuilders)
5154
{
5255
builder.AppendLine($" [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({codeBuilder.Namespace}.{codeBuilder.ClassName}))]");
5356
}
54-
builder.AppendLine(" [ModuleInitializer]");
57+
58+
var resultTypes = this.codeBuilders.SelectMany(item => item.GetResultTypes()).Distinct(SymbolEqualityComparer.Default);
59+
foreach(var resultType in resultTypes)
60+
{
61+
builder.AppendLine($" [DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, typeof(DefaultApiActionInvoker<{resultType}>))]");
62+
}
63+
5564
builder.AppendLine(" public static void AddDynamicDependency()");
5665
builder.AppendLine(" {");
5766
builder.AppendLine(" }");

WebApiClientCore.Analyzers.SourceGenerator/HttpApiCodeBuilder.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ public override string ToString()
128128
}
129129

130130

131+
public IEnumerable<ITypeSymbol> GetResultTypes()
132+
{
133+
var methods = HttpApiMethodFinder.FindApiMethods(this.httpApi);
134+
foreach (var method in methods)
135+
{
136+
if (method.ReturnType is INamedTypeSymbol typeSymbol)
137+
{
138+
var resultType = typeSymbol.TypeArguments.FirstOrDefault();
139+
if (resultType != null)
140+
{
141+
yield return resultType;
142+
}
143+
}
144+
}
145+
}
146+
131147
/// <summary>
132148
/// 构建方法
133149
/// </summary>

WebApiClientCore.Extensions.SourceGenerator/Implementations/SourceGeneratorHttpApiActivator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace WebApiClientCore.Implementations
1212
/// <typeparam name="THttpApi"></typeparam>
1313
public class SourceGeneratorHttpApiActivator<
1414
#if NET5_0_OR_GREATER
15-
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
15+
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
1616
#endif
1717
THttpApi> : IHttpApiActivator<THttpApi>
1818
{

WebApiClientCore/Implementations/DefaultApiActionInvoker.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ namespace WebApiClientCore.Implementations
1313
/// </summary>
1414
/// <typeparam name="TResult"></typeparam>
1515
[DebuggerDisplay("Member = {ActionDescriptor.Member}")]
16-
public class DefaultApiActionInvoker<
17-
#if NET5_0_OR_GREATER
18-
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
19-
#endif
20-
TResult> : ApiActionInvoker, IITaskReturnConvertable
16+
public class DefaultApiActionInvoker<TResult> : ApiActionInvoker, IITaskReturnConvertable
2117
{
2218
/// <summary>
2319
/// 获取Action描述
@@ -69,7 +65,7 @@ public virtual async Task<TResult> InvokeAsync(HttpClientContext context, object
6965

7066
var httpContext = new HttpContext(context, requestMessage);
7167
var requestContext = new ApiRequestContext(httpContext, this.ActionDescriptor, arguments, new DefaultDataCollection());
72-
return await this.InvokeAsync(requestContext).ConfigureAwait(false);
68+
return await InvokeAsync(requestContext).ConfigureAwait(false);
7369
}
7470
catch (HttpRequestException)
7571
{
@@ -87,7 +83,7 @@ public virtual async Task<TResult> InvokeAsync(HttpClientContext context, object
8783
/// </summary>
8884
/// <param name="request"></param>
8985
/// <returns></returns>
90-
private async Task<TResult> InvokeAsync(ApiRequestContext request)
86+
private static async Task<TResult> InvokeAsync(ApiRequestContext request)
9187
{
9288
#nullable disable
9389
var response = await ApiRequestExecuter.ExecuteAsync(request).ConfigureAwait(false);
@@ -140,7 +136,7 @@ public ITaskReturnActionInvoker(DefaultApiActionInvoker<TResult> actionInvoker)
140136
public override object Invoke(HttpClientContext context, object?[] arguments)
141137
{
142138
return new ActionTask<TResult>(this.actionInvoker, context, arguments);
143-
}
139+
}
144140
}
145141
}
146142
}

WebApiClientCore/Implementations/DefaultHttpApiActivator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace WebApiClientCore.Implementations
1313
/// <typeparam name="THttpApi"></typeparam>
1414
public class DefaultHttpApiActivator<
1515
#if NET5_0_OR_GREATER
16-
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.All)]
16+
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicConstructors)]
1717
#endif
1818
THttpApi> : IHttpApiActivator<THttpApi>
1919
{

0 commit comments

Comments
 (0)