Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Adding attributes to support Native AOT compiling #37734

Merged
merged 90 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 84 commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
e347a6a
part 1
m-redding Jul 19, 2023
0c247da
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Jul 19, 2023
dacab57
additional changes
m-redding Jul 19, 2023
466ef7b
additional attributes
m-redding Jul 20, 2023
07b5ec6
additional attributes
m-redding Jul 20, 2023
2c15eec
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Jul 24, 2023
581d888
adding diagnosticscope attributes
m-redding Jul 28, 2023
d1d99e7
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Jul 28, 2023
e6fa1e4
Merge branch 'resolve-aot-warnings-phase1' of https://github.com/m-re…
m-redding Jul 28, 2023
9d0ae69
updating messages
m-redding Jul 31, 2023
653623c
more fixes
m-redding Jul 31, 2023
5d9b2dc
additional updates
m-redding Jul 31, 2023
ddf556b
updates
m-redding Aug 3, 2023
44f0da5
PR feedback
m-redding Aug 4, 2023
f17c266
API update
m-redding Aug 4, 2023
540619f
removing unnecessary warnings
m-redding Aug 4, 2023
bf185ec
reverting attribute on HttpPipelineSynchronousPolicy
m-redding Aug 4, 2023
fcb6488
update api
m-redding Aug 4, 2023
7623768
updates
m-redding Aug 8, 2023
bdf71e9
update
m-redding Aug 8, 2023
d95f014
API export
m-redding Aug 9, 2023
032c14f
WIP fix 1
m-redding Aug 9, 2023
47228e8
WIP
m-redding Aug 9, 2023
47981b3
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Aug 9, 2023
f00379d
attribute adjustments
m-redding Aug 9, 2023
2b64b23
adding compatibility test
m-redding Aug 10, 2023
ebcd377
WIP adding test
m-redding Aug 14, 2023
ba7bcfe
WIP API
m-redding Aug 14, 2023
b074259
WIP
m-redding Aug 14, 2023
2d53367
WIP
m-redding Aug 14, 2023
d1010d2
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Aug 14, 2023
f879d9c
updates
m-redding Aug 14, 2023
e9253a0
add test script and remove #if statements
m-redding Aug 15, 2023
5473799
fixing build
m-redding Aug 15, 2023
f2c2543
Merge branch 'main' into resolve-aot-warnings-phase1
m-redding Aug 15, 2023
93bdee1
Update Program.cs
m-redding Aug 15, 2023
edbe071
adding some compile statements
m-redding Aug 15, 2023
f050d2e
Update AzureCoreEventSource.cs
m-redding Aug 15, 2023
ec1d84e
build fix attempt
m-redding Aug 17, 2023
3579eea
fix built attempt 2
m-redding Aug 17, 2023
5817ccc
Update MutableJsonChange.cs
m-redding Aug 18, 2023
9fa60a5
Update MutableJsonChange.cs
m-redding Aug 18, 2023
1a589df
Update MutableJsonDocument.cs
m-redding Aug 18, 2023
1d6986b
Update HttpPipelineSynchronousPolicy.cs
m-redding Aug 18, 2023
d78b8ef
adding comments
m-redding Aug 18, 2023
176afe6
PR fb 1
m-redding Aug 18, 2023
461c072
test
m-redding Aug 22, 2023
496b809
test2
m-redding Aug 22, 2023
f596df5
test3
m-redding Aug 22, 2023
a1751ce
Merge branch 'main' into resolve-aot-warnings-phase1
m-redding Aug 24, 2023
ebfc02d
attempt
m-redding Aug 28, 2023
553148c
Merge branch 'resolve-aot-warnings-phase1' of https://github.com/m-re…
m-redding Aug 28, 2023
0b83cb8
additional build fixes
m-redding Aug 29, 2023
27085c0
Remove comment
m-redding Aug 29, 2023
44b1bee
PR feedback + fixing impacts of feedback changes
m-redding Sep 7, 2023
9ebbf94
fixes
m-redding Sep 7, 2023
f6fef75
Update GeoRedundantFallbackPolicy.cs
m-redding Sep 7, 2023
8e4b7bb
Update GeoRedundantFallbackPolicy.cs
m-redding Sep 7, 2023
69cf078
Update GeoRedundantFallbackPolicy.cs
m-redding Sep 7, 2023
be791c5
fix
m-redding Sep 7, 2023
917647e
Merge branch 'main' into resolve-aot-warnings-phase1
m-redding Sep 7, 2023
b2a25e2
add tests
m-redding Sep 7, 2023
41040dd
pipeline fixes
m-redding Sep 7, 2023
8c0aa83
update comments
m-redding Sep 7, 2023
e9c06b3
feedback
m-redding Sep 7, 2023
83c118f
adjustments
m-redding Sep 7, 2023
0f6e866
adjustment
m-redding Sep 7, 2023
b677aec
pipeline fix
m-redding Sep 7, 2023
a211515
PR feedback 1
m-redding Sep 8, 2023
1026bc1
Apply suggestions from code review
m-redding Sep 8, 2023
b212d9e
PR feedback updates 2
m-redding Sep 8, 2023
1a12f76
Update sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs
m-redding Sep 8, 2023
6c86362
need one other suppression
m-redding Sep 8, 2023
c25e250
remove 2 annotations + update suppression message
m-redding Sep 11, 2023
83d4650
removing all attributes from RequestContentHelper
m-redding Sep 11, 2023
ac9c480
tweak justification
m-redding Sep 12, 2023
23eea94
removing test project from Azure.Core, will be adding feedback to gen…
m-redding Sep 13, 2023
1969119
WIP
m-redding Sep 14, 2023
cc4a4a8
feedback
m-redding Sep 14, 2023
004ced0
Update sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs
m-redding Sep 14, 2023
af15022
fix attribute and add comments
m-redding Sep 15, 2023
9938471
Merge branch 'resolve-aot-warnings-phase1' of https://github.com/m-re…
m-redding Sep 15, 2023
992ef22
Addressing feedback
m-redding Sep 25, 2023
00d5db1
adding net 5 preprocessor directives
m-redding Sep 26, 2023
d5ac8ee
Update sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs
m-redding Sep 27, 2023
12c34f3
remove unused const
m-redding Sep 27, 2023
d2c8865
Merge branch 'resolve-aot-warnings-phase1' of https://github.com/m-re…
m-redding Sep 27, 2023
60e4c86
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Sep 27, 2023
9838582
Merge branch 'Azure:main' into resolve-aot-warnings-phase1
m-redding Sep 27, 2023
be02829
pipeline fix
m-redding Sep 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/Directory.Build.Common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<Compile Include="$(AzureCoreSharedSources)ChangeTrackingDictionary.cs" LinkBase="Shared/Core" />
<Compile Include="$(AzureCoreSharedSources)ChangeTrackingList.cs" LinkBase="Shared/Core" />
<Compile Include="$(AzureCoreSharedSources)ClientDiagnostics.cs" LinkBase="Shared/Core" />
<Compile Include="$(AzureCoreSharedSources)TrimmingAttribute.cs" LinkBase="Shared/Core" />
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
<Compile Include="$(AzureCoreSharedSources)DiagnosticScope.cs" LinkBase="Shared/Core" />
<Compile Include="$(AzureCoreSharedSources)DiagnosticScopeFactory.cs" LinkBase="Shared/Core" />
<Compile Include="$(AzureCoreSharedSources)FixedDelayWithNoJitterStrategy.cs" LinkBase="Shared/Core" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
<Compile Include="$(AzureCoreSharedSources)PropertyReferenceTypeAttribute.cs" LinkBase="Shared\Core" />
<Compile Include="$(AzureCoreSharedSources)InitializationConstructorAttribute.cs" LinkBase="Shared\Core" />
<Compile Include="$(AzureCoreSharedSources)SerializationConstructorAttribute.cs" LinkBase="Shared\Core" />
<Compile Include="$(AzureCoreSharedSources)TrimmingAttribute.cs" LinkBase="Shared\Core" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net5.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,11 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
annelo-msft marked this conversation as resolved.
Show resolved Hide resolved
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.JsonPropertyNames propertyNameFormat, string dateTimeFormat = "o") { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.ObjectSerializer? serializer) { throw null; }
public static Azure.Core.RequestContent Create(System.ReadOnlyMemory<byte> bytes) { throw null; }
public static Azure.Core.RequestContent Create(string content) { throw null; }
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,11 @@ protected RequestContent() { }
public static Azure.Core.RequestContent Create(byte[] bytes) { throw null; }
public static Azure.Core.RequestContent Create(byte[] bytes, int index, int length) { throw null; }
public static Azure.Core.RequestContent Create(System.IO.Stream stream) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable) { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.JsonPropertyNames propertyNameFormat, string dateTimeFormat = "o") { throw null; }
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This method uses reflection-based serialization which is incompatible with trimming. Try using one of the 'Create' overloads that doesn't wrap a serialized version of an object.")]
public static Azure.Core.RequestContent Create(object serializable, Azure.Core.Serialization.ObjectSerializer? serializer) { throw null; }
public static Azure.Core.RequestContent Create(System.ReadOnlyMemory<byte> bytes) { throw null; }
public static Azure.Core.RequestContent Create(string content) { throw null; }
Expand Down Expand Up @@ -1024,6 +1027,7 @@ protected HttpPipelinePolicy() { }
protected static void ProcessNext(Azure.Core.HttpMessage message, System.ReadOnlyMemory<Azure.Core.Pipeline.HttpPipelinePolicy> pipeline) { }
protected static System.Threading.Tasks.ValueTask ProcessNextAsync(Azure.Core.HttpMessage message, System.ReadOnlyMemory<Azure.Core.Pipeline.HttpPipelinePolicy> pipeline) { throw null; }
}
[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
public abstract partial class HttpPipelineSynchronousPolicy : Azure.Core.Pipeline.HttpPipelinePolicy
{
protected HttpPipelineSynchronousPolicy() { }
Expand Down Expand Up @@ -1075,6 +1079,7 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce
}
namespace Azure.Core.Serialization
{
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming.")]
[System.Diagnostics.DebuggerDisplayAttribute("{DebuggerDisplay,nq}")]
public sealed partial class DynamicData : System.Dynamic.IDynamicMetaObjectProvider, System.IDisposable
{
Expand Down Expand Up @@ -1109,6 +1114,7 @@ public partial interface IMemberNameConverter
{
string? ConvertMemberName(System.Reflection.MemberInfo member);
}
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("This class uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
m-redding marked this conversation as resolved.
Show resolved Hide resolved
public partial class JsonObjectSerializer : Azure.Core.Serialization.ObjectSerializer, Azure.Core.Serialization.IMemberNameConverter
{
public JsonObjectSerializer() { }
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/src/Diagnostics/AzureCoreEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Azure.Core.Diagnostics
Expand Down Expand Up @@ -231,6 +232,7 @@ public void ErrorResponseContentTextBlock(string requestId, int blockNumber, str
}

[Event(RequestRetryingEvent, Level = EventLevel.Informational, Message = "Request [{0}] attempt number {1} took {2:00.0}s")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "WriteEvent is used with primitive types.")]
public void RequestRetrying(string requestId, int retryNumber, double seconds)
{
WriteEvent(RequestRetryingEvent, requestId, retryNumber, seconds);
Expand Down
14 changes: 14 additions & 0 deletions sdk/core/Azure.Core/src/DynamicData/DynamicData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core.Json;
Expand All @@ -20,6 +22,10 @@ namespace Azure.Core.Serialization
/// This and related types are not intended to be mocked.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
m-redding marked this conversation as resolved.
Show resolved Hide resolved
[RequiresUnreferencedCode(MutableJsonDocument.SerializationRequiresUnreferencedCodeClass)]
[RequiresDynamicCode(MutableJsonDocument.SerializationRequiresUnreferencedCodeClass)]
#endif
[JsonConverter(typeof(DynamicDataJsonConverter))]
public sealed partial class DynamicData : IDisposable
{
Expand All @@ -33,6 +39,8 @@ public sealed partial class DynamicData : IDisposable
private readonly DynamicDataOptions _options;
private readonly JsonSerializerOptions _serializerOptions;

internal const string SerializationRequiresUnreferencedCodeClass = "This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming.";

internal DynamicData(MutableJsonElement element, DynamicDataOptions options)
{
_element = element;
Expand Down Expand Up @@ -471,8 +479,14 @@ public override int GetHashCode()
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => _element.DebuggerDisplay;

#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
[RequiresUnreferencedCode(ClassIsIncompatibleWithTrimming)]
#endif
[RequiresDynamicCode(ClassIsIncompatibleWithTrimming)]
private class DynamicDataJsonConverter : JsonConverter<DynamicData>
{
public const string ClassIsIncompatibleWithTrimming = "Using DynamicData or DynamicDataConverter is not compatible with trimming due to reflection-based serialization.";

public override DynamicData Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
MutableJsonDocument mdoc = MutableJsonDocument.Parse(ref reader);
Expand Down
3 changes: 3 additions & 0 deletions sdk/core/Azure.Core/src/DynamicData/MutableJsonChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;

#nullable enable
Expand All @@ -10,6 +11,8 @@ namespace Azure.Core.Json
{
internal struct MutableJsonChange
{
internal const string SerializationRequiresUnreferencedCode = "This method utilizes reflection-based JSON serialization which is not compatible with trimming.";

m-redding marked this conversation as resolved.
Show resolved Hide resolved
public MutableJsonChange(string path,
int index,
object? value,
Expand Down
16 changes: 16 additions & 0 deletions sdk/core/Azure.Core/src/DynamicData/MutableJsonDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Text.Json;
Expand All @@ -15,6 +16,13 @@ namespace Azure.Core.Json
/// <summary>
/// A mutable representation of a JSON value.
/// </summary>
#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
// This class is marked as RequiresUnreferencedCode and RequiresDynamic code for two reasons. First, for the usage of MutableJsonElement in RootElement and WriteTo. Second, the method WriteTo
// is used in the MutableJsonDocumentConverter, which causes MutableJsonDocumentConverter to be incompatible with trimming. Since the class is used in the attribute on this class, the entire
// class must be annotated.
[RequiresUnreferencedCode(SerializationRequiresUnreferencedCodeClass)]
[RequiresDynamicCode(SerializationRequiresUnreferencedCodeClass)]
#endif
[JsonConverter(typeof(MutableJsonDocumentConverter))]
internal sealed partial class MutableJsonDocument : IDisposable
{
Expand All @@ -26,6 +34,8 @@ internal sealed partial class MutableJsonDocument : IDisposable
private readonly JsonSerializerOptions _serializerOptions;
internal JsonSerializerOptions SerializerOptions => _serializerOptions;

internal const string SerializationRequiresUnreferencedCodeClass = "This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming.";

private ChangeTracker? _changeTracker;
internal ChangeTracker Changes
{
Expand Down Expand Up @@ -210,8 +220,14 @@ private MutableJsonDocument(JsonDocument document, ReadOnlyMemory<byte> utf8Json
_serializerOptions = serializerOptions ?? DefaultSerializerOptions;
}

#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
[RequiresUnreferencedCode(classIsIncompatibleWithTrimming)]
#endif
[RequiresDynamicCode(classIsIncompatibleWithTrimming)]
private class MutableJsonDocumentConverter : JsonConverter<MutableJsonDocument>
{
public const string classIsIncompatibleWithTrimming = "Using MutableJsonDocument or MutableJsonDocumentConverter is not compatible with trimming due to reflection-based serialization.";

public override MutableJsonDocument Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return Parse(ref reader);
Expand Down
21 changes: 21 additions & 0 deletions sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Text.Json;
Expand All @@ -17,6 +18,12 @@ namespace Azure.Core.Json
/// A mutable representation of a JSON element.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
// This struct is incompatible with trimming for a couple reasons:
// 1. MutableJsonElementConverter is not compatible with trimming, since it is used in an attribute, the whole struct is incompatible with trimming.
// 2. The internal static method SerializeToJsonElement uses reflection-based serialization. This method is used throughout the non-static methods within this struct.
// In order to avoid annotating every single method, we are instead annotating the constructor, and the problematic static method.
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "The constructor is marked as RequiresUnreferencedCode, since the whole struct is incompatible with trimming.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL3050", Justification = "The constructor is marked as RequiresDynamicCode, since the whole struct is incompatible with trimming.")]
m-redding marked this conversation as resolved.
Show resolved Hide resolved
[JsonConverter(typeof(MutableJsonElementConverter))]
internal readonly partial struct MutableJsonElement
{
Expand All @@ -27,8 +34,15 @@ internal readonly partial struct MutableJsonElement
private readonly string _path;
private readonly int _highWaterMark;

internal const string SerializationRequiresUnreferencedCodeConstructor = "This struct utilizes reflection-based JSON serialization which is not compatible with trimming.";
internal const string SerializationRequiresUnreferencedCodeMethod = "This method utilizes reflection-based JSON serialization which is not compatible with trimming.";

private readonly MutableJsonDocument.ChangeTracker Changes => _root.Changes;

// The default constructor generated by the compiler should also be annotated - but since this struct cannot function if the default constructor is used, it is okay that
// only this constructor is annotated.
[RequiresUnreferencedCode(SerializationRequiresUnreferencedCodeConstructor)]
m-redding marked this conversation as resolved.
Show resolved Hide resolved
m-redding marked this conversation as resolved.
Show resolved Hide resolved
[RequiresDynamicCode(SerializationRequiresUnreferencedCodeConstructor)]
internal MutableJsonElement(MutableJsonDocument root, JsonElement element, string path, int highWaterMark = -1)
{
_element = element;
Expand Down Expand Up @@ -1302,6 +1316,10 @@ public override string ToString()
return _element.ToString() ?? "null";
}

#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
m-redding marked this conversation as resolved.
Show resolved Hide resolved
[RequiresUnreferencedCode(SerializationRequiresUnreferencedCodeMethod)]
[RequiresDynamicCode(SerializationRequiresUnreferencedCodeMethod)]
#endif
internal static JsonElement SerializeToJsonElement(object? value, JsonSerializerOptions? options = default)
{
byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(value, options);
Expand Down Expand Up @@ -1395,6 +1413,9 @@ private void EnsureValid()
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal string DebuggerDisplay => $"ValueKind = {ValueKind} : \"{ToString()}\"";

#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
[RequiresUnreferencedCode("Using MutableJsonElement or MutableJsonElementConverter is not compatible with trimming due to reflection-based serialization.")]
#endif
private class MutableJsonElementConverter : JsonConverter<MutableJsonElement>
{
public override MutableJsonElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Threading.Tasks;

Expand All @@ -11,6 +12,9 @@ namespace Azure.Core.Pipeline
/// <summary>
/// Represents a <see cref="HttpPipelinePolicy"/> that doesn't do any asynchronous or synchronously blocking operations.
/// </summary>
#if !NET5_0 // DynamicallyAccessedMembers in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0
[DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)]
#endif
public abstract class HttpPipelineSynchronousPolicy : HttpPipelinePolicy
{
private static Type[] _onReceivedResponseParameters = new[] { typeof(HttpMessage) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading.Tasks;

Expand Down Expand Up @@ -58,11 +59,7 @@ public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePol

private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
{
#if NETCOREAPP2_1
using var scope = new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, message, s_activitySource, DiagnosticScope.ActivityKind.Client, false);
#else
using var scope = new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, message, s_activitySource, System.Diagnostics.ActivityKind.Client, false);
#endif
using var scope = CreateDiagnosticScope(message);

bool isActivitySourceEnabled = IsActivitySourceEnabled;

Expand Down Expand Up @@ -136,6 +133,16 @@ private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPip
}
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "The values being passed into Write have the commonly used properties being preserved with DynamicallyAccessedMembers.")]
private DiagnosticScope CreateDiagnosticScope<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(T sourceArgs)
{
#if NETCOREAPP2_1
return new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, sourceArgs, s_activitySource, DiagnosticScope.ActivityKind.Client, false);
#else
return new DiagnosticScope("Azure.Core.Http.Request", s_diagnosticSource, sourceArgs, s_activitySource, System.Diagnostics.ActivityKind.Client, false);
#endif
}

private static ValueTask ProcessNextAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
{
Activity? currentActivity = Activity.Current;
Expand Down
Loading