Skip to content

Commit 22bba92

Browse files
committed
消除aot警告
1 parent fa9e933 commit 22bba92

File tree

10 files changed

+138
-20
lines changed

10 files changed

+138
-20
lines changed

AppAot/AppHostedService.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,23 @@
77
namespace AppAot
88
{
99
class AppHostedService : BackgroundService
10-
{
10+
{
1111
private readonly IServiceScopeFactory serviceScopeFactory;
1212
private readonly ILogger<AppHostedService> logger;
1313

14-
public AppHostedService(
14+
public AppHostedService(
1515
IServiceScopeFactory serviceScopeFactory,
1616
ILogger<AppHostedService> logger)
17-
{
17+
{
1818
this.serviceScopeFactory = serviceScopeFactory;
1919
this.logger = logger;
2020
}
2121

2222
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
23-
{
23+
{
2424
using var scope = this.serviceScopeFactory.CreateScope();
2525
var api = scope.ServiceProvider.GetRequiredService<ICloudflareApi>();
2626
var appData = await api.GetAppDataAsync();
27-
appData = await api.GetAppData2Async();
2827
this.logger.LogInformation($"WebpackCompilationHash: {appData.WebpackCompilationHash}");
2928
}
3029
}

AppAot/ICloudflareApi.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ namespace AppAot
99
public interface ICloudflareApi
1010
{
1111
[HttpGet("/page-data/app-data.json")]
12-
Task<AppData> GetAppDataAsync();
13-
14-
[HttpGet("/page-data/app-data.json")]
15-
ITask<AppData> GetAppData2Async();
12+
Task<AppData> GetAppDataAsync();
1613
}
1714
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if NETSTANDARD2_1 || NET5_0
2+
namespace System.Diagnostics.CodeAnalysis
3+
{
4+
/// <summary>
5+
/// Specifies that the attributed class, constructor, or method requires dynamic code.
6+
/// </summary>
7+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
8+
sealed class RequiresDynamicCodeAttribute : Attribute
9+
{
10+
/// <summary>
11+
/// Gets the message associated with the requirement for dynamic code.
12+
/// </summary>
13+
public string Message { get; }
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RequiresDynamicCodeAttribute"/> class with the specified message.
17+
/// </summary>
18+
/// <param name="message">The message associated with the requirement for dynamic code.</param>
19+
public RequiresDynamicCodeAttribute(string message)
20+
{
21+
this.Message = message;
22+
}
23+
}
24+
}
25+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if NETSTANDARD2_1
2+
namespace System.Diagnostics.CodeAnalysis
3+
{
4+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
5+
sealed class RequiresUnreferencedCodeAttribute : Attribute
6+
{
7+
/// <summary>
8+
/// 获取或设置对于未引用代码的要求的消息。
9+
/// </summary>
10+
public string Message { get; }
11+
12+
/// <summary>
13+
/// 初始化 <see cref="RequiresUnreferencedCodeAttribute"/> 类的新实例。
14+
/// </summary>
15+
/// <param name="message">对于未引用代码的要求的消息。</param>
16+
public RequiresUnreferencedCodeAttribute(string message)
17+
{
18+
this.Message = message;
19+
}
20+
}
21+
}
22+
#endif

WebApiClientCore.Abstractions/HttpApiOptions.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Runtime.CompilerServices;
35
using System.Text.Encodings.Web;
46
using System.Text.Json;
57
using System.Xml;
@@ -96,10 +98,14 @@ private static JsonSerializerOptions CreateJsonSerializeOptions()
9698
/// 创建反序列化JsonSerializerOptions
9799
/// </summary>
98100
/// <returns></returns>
101+
[UnconditionalSuppressMessage("Trimming", "IL3050", Justification = "JsonCompatibleConverter.EnumReader使用前已经判断RuntimeFeature.IsDynamicCodeSupported")]
99102
private static JsonSerializerOptions CreateJsonDeserializeOptions()
100103
{
101104
var options = CreateJsonSerializeOptions();
102-
options.Converters.Add(JsonCompatibleConverter.EnumReader);
105+
if (RuntimeFeature.IsDynamicCodeSupported)
106+
{
107+
options.Converters.Add(JsonCompatibleConverter.EnumReader);
108+
}
103109
options.Converters.Add(JsonCompatibleConverter.DateTimeReader);
104110
return options;
105111
}

WebApiClientCore.Abstractions/Serialization/JsonConverters/JsonCompatibleConverter.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text.Json.Serialization;
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Text.Json.Serialization;
23

34
namespace WebApiClientCore.Serialization.JsonConverters
45
{
@@ -7,10 +8,20 @@ namespace WebApiClientCore.Serialization.JsonConverters
78
/// </summary>
89
public static class JsonCompatibleConverter
910
{
11+
private static JsonStringEnumConverter? stringEnumConverter;
12+
1013
/// <summary>
1114
/// 获取Enum类型反序列化兼容的转换器
1215
/// </summary>
13-
public static JsonConverter EnumReader { get; } = new JsonStringEnumConverter();
16+
public static JsonConverter EnumReader
17+
{
18+
[RequiresDynamicCode("JsonStringEnumConverter需要动态代码")]
19+
get
20+
{
21+
stringEnumConverter ??= new JsonStringEnumConverter();
22+
return stringEnumConverter;
23+
}
24+
}
1425

1526
/// <summary>
1627
/// 获取DateTime类型反序列化兼容的转换器
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if NETSTANDARD2_1 || NET5_0
2+
namespace System.Diagnostics.CodeAnalysis
3+
{
4+
/// <summary>
5+
/// Specifies that the attributed class, constructor, or method requires dynamic code.
6+
/// </summary>
7+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
8+
sealed class RequiresDynamicCodeAttribute : Attribute
9+
{
10+
/// <summary>
11+
/// Gets the message associated with the requirement for dynamic code.
12+
/// </summary>
13+
public string Message { get; }
14+
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="RequiresDynamicCodeAttribute"/> class with the specified message.
17+
/// </summary>
18+
/// <param name="message">The message associated with the requirement for dynamic code.</param>
19+
public RequiresDynamicCodeAttribute(string message)
20+
{
21+
this.Message = message;
22+
}
23+
}
24+
}
25+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#if NETSTANDARD2_1
2+
namespace System.Diagnostics.CodeAnalysis
3+
{
4+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
5+
sealed class RequiresUnreferencedCodeAttribute : Attribute
6+
{
7+
/// <summary>
8+
/// 获取或设置对于未引用代码的要求的消息。
9+
/// </summary>
10+
public string Message { get; }
11+
12+
/// <summary>
13+
/// 初始化 <see cref="RequiresUnreferencedCodeAttribute"/> 类的新实例。
14+
/// </summary>
15+
/// <param name="message">对于未引用代码的要求的消息。</param>
16+
public RequiresUnreferencedCodeAttribute(string message)
17+
{
18+
this.Message = message;
19+
}
20+
}
21+
}
22+
#endif

WebApiClientCore.OpenApi.SourceGenerator/HttpApiSettings.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public HttpApiSettings()
2929
this.ResponseArrayType = "List";
3030
this.ResponseDictionaryType = "Dictionary";
3131
this.ParameterArrayType = "IEnumerable";
32-
this.ParameterDictionaryType = "IDictionary";
32+
this.ParameterDictionaryType = "IDictionary";
3333

3434
this.OperationNameGenerator = new OperationNameProvider();
3535
this.ParameterNameGenerator = new ParameterNameProvider();
@@ -157,8 +157,8 @@ private static string PrettyName(string name)
157157
name = name.Replace("[]", "Array");
158158
}
159159

160-
var matchs = Regex.Matches(name, @"\W");
161-
if (matchs.Count == 0 || matchs.Count % 2 > 0)
160+
var matches = Regex.Matches(name, @"\W");
161+
if (matches.Count == 0 || matches.Count % 2 > 0)
162162
{
163163
return name;
164164
}
@@ -167,7 +167,7 @@ private static string PrettyName(string name)
167167
return Regex.Replace(name, @"\W", m =>
168168
{
169169
index++;
170-
return index < matchs.Count / 2 ? "Of" : null;
170+
return index < matches.Count / 2 ? "Of" : string.Empty;
171171
});
172172
}
173173
}

WebApiClientCore/Implementations/DefaultHttpApiActivator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.CompilerServices;
4+
using WebApiClientCore.Exceptions;
35

46
namespace WebApiClientCore.Implementations
57
{
@@ -21,12 +23,21 @@ public class DefaultHttpApiActivator<[DynamicallyAccessedMembers(DynamicallyAcce
2123
/// <param name="actionInvokerProvider"></param>
2224
/// <exception cref="ArgumentException"></exception>
2325
/// <exception cref="NotSupportedException"></exception>
24-
[UnconditionalSuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.", Justification = "<Pending>")]
26+
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "ILEmitHttpApiActivator使用之前已经使用RuntimeFeature.IsDynamicCodeCompiled来判断")]
2527
public DefaultHttpApiActivator(IApiActionDescriptorProvider apiActionDescriptorProvider, IApiActionInvokerProvider actionInvokerProvider)
2628
{
27-
this.httpApiActivator = SourceGeneratorHttpApiActivator<THttpApi>.IsSupported
28-
? new SourceGeneratorHttpApiActivator<THttpApi>(apiActionDescriptorProvider, actionInvokerProvider)
29-
: new ILEmitHttpApiActivator<THttpApi>(apiActionDescriptorProvider, actionInvokerProvider);
29+
if (SourceGeneratorHttpApiActivator<THttpApi>.IsSupported)
30+
{
31+
this.httpApiActivator = new SourceGeneratorHttpApiActivator<THttpApi>(apiActionDescriptorProvider, actionInvokerProvider);
32+
}
33+
else if (RuntimeFeature.IsDynamicCodeCompiled)
34+
{
35+
this.httpApiActivator = new ILEmitHttpApiActivator<THttpApi>(apiActionDescriptorProvider, actionInvokerProvider);
36+
}
37+
else
38+
{
39+
throw new ProxyTypeCreateException(typeof(HttpApi));
40+
}
3041
}
3142

3243
/// <summary>

0 commit comments

Comments
 (0)