diff --git a/eng/Directory.Build.Common.targets b/eng/Directory.Build.Common.targets index 1a2a3195bf2f..2cb1deaf78ce 100644 --- a/eng/Directory.Build.Common.targets +++ b/eng/Directory.Build.Common.targets @@ -201,6 +201,7 @@ + diff --git a/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj b/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj index b9371f9bb9bf..f67b3c8958da 100644 --- a/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj +++ b/sdk/core/Azure.Core.Expressions.DataFactory/src/Azure.Core.Expressions.DataFactory.csproj @@ -32,6 +32,7 @@ + diff --git a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs index c10168bea2ae..a1bbd8de4d83 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs @@ -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 bytes) { throw null; } public static Azure.Core.RequestContent Create(string content) { throw null; } diff --git a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs index c10168bea2ae..dec24e418149 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs @@ -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 bytes) { throw null; } public static Azure.Core.RequestContent Create(string content) { throw null; } @@ -1024,6 +1027,7 @@ protected HttpPipelinePolicy() { } protected static void ProcessNext(Azure.Core.HttpMessage message, System.ReadOnlyMemory pipeline) { } protected static System.Threading.Tasks.ValueTask ProcessNextAsync(Azure.Core.HttpMessage message, System.ReadOnlyMemory pipeline) { throw null; } } + [System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicMethods)] public abstract partial class HttpPipelineSynchronousPolicy : Azure.Core.Pipeline.HttpPipelinePolicy { protected HttpPipelineSynchronousPolicy() { } @@ -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 { @@ -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.")] public partial class JsonObjectSerializer : Azure.Core.Serialization.ObjectSerializer, Azure.Core.Serialization.IMemberNameConverter { public JsonObjectSerializer() { } diff --git a/sdk/core/Azure.Core/src/Diagnostics/AzureCoreEventSource.cs b/sdk/core/Azure.Core/src/Diagnostics/AzureCoreEventSource.cs index d9e5c2846e54..981906f38e9f 100644 --- a/sdk/core/Azure.Core/src/Diagnostics/AzureCoreEventSource.cs +++ b/sdk/core/Azure.Core/src/Diagnostics/AzureCoreEventSource.cs @@ -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 @@ -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); diff --git a/sdk/core/Azure.Core/src/DynamicData/DynamicData.cs b/sdk/core/Azure.Core/src/DynamicData/DynamicData.cs index 1a978bcf7a0f..46e51281edae 100644 --- a/sdk/core/Azure.Core/src/DynamicData/DynamicData.cs +++ b/sdk/core/Azure.Core/src/DynamicData/DynamicData.cs @@ -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; @@ -20,6 +22,10 @@ namespace Azure.Core.Serialization /// This and related types are not intended to be mocked. /// [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 + [RequiresUnreferencedCode(MutableJsonDocument.SerializationRequiresUnreferencedCodeClass)] + [RequiresDynamicCode(MutableJsonDocument.SerializationRequiresUnreferencedCodeClass)] +#endif [JsonConverter(typeof(DynamicDataJsonConverter))] public sealed partial class DynamicData : IDisposable { @@ -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; @@ -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 { + 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); diff --git a/sdk/core/Azure.Core/src/DynamicData/MutableJsonChange.cs b/sdk/core/Azure.Core/src/DynamicData/MutableJsonChange.cs index 688dfa46b767..72163a3bcdc5 100644 --- a/sdk/core/Azure.Core/src/DynamicData/MutableJsonChange.cs +++ b/sdk/core/Azure.Core/src/DynamicData/MutableJsonChange.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System; +using System.Diagnostics.CodeAnalysis; using System.Text.Json; #nullable enable diff --git a/sdk/core/Azure.Core/src/DynamicData/MutableJsonDocument.cs b/sdk/core/Azure.Core/src/DynamicData/MutableJsonDocument.cs index a9795c8e0de6..97e92a8fe02d 100644 --- a/sdk/core/Azure.Core/src/DynamicData/MutableJsonDocument.cs +++ b/sdk/core/Azure.Core/src/DynamicData/MutableJsonDocument.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using System.Text.Json; @@ -15,6 +16,13 @@ namespace Azure.Core.Json /// /// A mutable representation of a JSON value. /// +#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 { @@ -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 { @@ -210,8 +220,14 @@ private MutableJsonDocument(JsonDocument document, ReadOnlyMemory 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 { + 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); diff --git a/sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs b/sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs index 9b474f11a3eb..265bb51c85be 100644 --- a/sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs +++ b/sdk/core/Azure.Core/src/DynamicData/MutableJsonElement.cs @@ -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; @@ -17,6 +18,12 @@ namespace Azure.Core.Json /// A mutable representation of a JSON element. /// [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 AOT.")] [JsonConverter(typeof(MutableJsonElementConverter))] internal readonly partial struct MutableJsonElement { @@ -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)] + [RequiresDynamicCode(SerializationRequiresUnreferencedCodeConstructor)] internal MutableJsonElement(MutableJsonDocument root, JsonElement element, string path, int highWaterMark = -1) { _element = element; @@ -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 + [RequiresUnreferencedCode(SerializationRequiresUnreferencedCodeMethod)] + [RequiresDynamicCode(SerializationRequiresUnreferencedCodeMethod)] +#endif internal static JsonElement SerializeToJsonElement(object? value, JsonSerializerOptions? options = default) { byte[] bytes = JsonSerializer.SerializeToUtf8Bytes(value, options); @@ -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 { public override MutableJsonElement Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) diff --git a/sdk/core/Azure.Core/src/Pipeline/HttpPipelineSynchronousPolicy.cs b/sdk/core/Azure.Core/src/Pipeline/HttpPipelineSynchronousPolicy.cs index e24d9fd75c08..14bb7d211221 100644 --- a/sdk/core/Azure.Core/src/Pipeline/HttpPipelineSynchronousPolicy.cs +++ b/sdk/core/Azure.Core/src/Pipeline/HttpPipelineSynchronousPolicy.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Threading.Tasks; @@ -11,6 +12,9 @@ namespace Azure.Core.Pipeline /// /// Represents a that doesn't do any asynchronous or synchronously blocking operations. /// +#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) }; diff --git a/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs b/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs index dc036f01faf6..e21101f8ff6b 100644 --- a/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs +++ b/sdk/core/Azure.Core/src/Pipeline/Internal/RequestActivityPolicy.cs @@ -3,6 +3,7 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Threading.Tasks; @@ -58,11 +59,7 @@ public override void Process(HttpMessage message, ReadOnlyMemory 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; @@ -136,6 +133,16 @@ private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory(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 pipeline, bool async) { Activity? currentActivity = Activity.Current; diff --git a/sdk/core/Azure.Core/src/RequestContent.cs b/sdk/core/Azure.Core/src/RequestContent.cs index fe19517dd70c..439b2aa0178f 100644 --- a/sdk/core/Azure.Core/src/RequestContent.cs +++ b/sdk/core/Azure.Core/src/RequestContent.cs @@ -3,6 +3,7 @@ using System; using System.Buffers; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; using System.Text.Json; @@ -18,6 +19,7 @@ namespace Azure.Core /// public abstract class RequestContent : IDisposable { + internal const string SerializationRequiresUnreferencedCode = "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."; private static readonly Encoding s_UTF8NoBomEncoding = new UTF8Encoding(false); /// @@ -84,6 +86,8 @@ public abstract class RequestContent : IDisposable /// /// The to serialize. /// An instance of that wraps a serialized version of the object. + [RequiresUnreferencedCode(SerializationRequiresUnreferencedCode)] + [RequiresDynamicCode(SerializationRequiresUnreferencedCode)] public static RequestContent Create(object serializable) => Create(serializable, JsonObjectSerializer.Default); /// @@ -92,6 +96,8 @@ public abstract class RequestContent : IDisposable /// The to serialize. /// The to use to convert the object to bytes. If not provided, is used. /// An instance of that wraps a serialized version of the object. + [RequiresUnreferencedCode(SerializationRequiresUnreferencedCode)] + [RequiresDynamicCode(SerializationRequiresUnreferencedCode)] public static RequestContent Create(object serializable, ObjectSerializer? serializer) => Create((serializer ?? JsonObjectSerializer.Default).Serialize(serializable)); /// @@ -101,6 +107,8 @@ public abstract class RequestContent : IDisposable /// The format to use for property names in the serialized content. /// The format to use for DateTime and DateTimeOffset values in the serialized content. /// An instance of that wraps a serialized version of the object. + [RequiresUnreferencedCode(SerializationRequiresUnreferencedCode)] + [RequiresDynamicCode(SerializationRequiresUnreferencedCode)] public static RequestContent Create(object serializable, JsonPropertyNames propertyNameFormat, string dateTimeFormat = DynamicData.RoundTripFormat) { DynamicDataOptions options = new() diff --git a/sdk/core/Azure.Core/src/Serialization/JsonObjectSerializer.cs b/sdk/core/Azure.Core/src/Serialization/JsonObjectSerializer.cs index b1c71f26409d..66832c6e2b9e 100644 --- a/sdk/core/Azure.Core/src/Serialization/JsonObjectSerializer.cs +++ b/sdk/core/Azure.Core/src/Serialization/JsonObjectSerializer.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Concurrent; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Reflection; using System.Text.Json; @@ -15,6 +16,10 @@ namespace Azure.Core.Serialization /// /// An implementation that uses for serialization/deserialization. /// +#if !NET5_0 // RequiresUnreferencedCode in net5.0 doesn't have AttributeTargets.Class as a target, but it was added in net6.0 + [RequiresUnreferencedCode("This class uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] +#endif + [RequiresDynamicCode("This class uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] public class JsonObjectSerializer : ObjectSerializer, IMemberNameConverter { private const int JsonIgnoreConditionAlways = 1; diff --git a/sdk/core/Azure.Core/src/Shared/ClientDiagnostics.cs b/sdk/core/Azure.Core/src/Shared/ClientDiagnostics.cs index 81a1859b1b97..c6e08d413199 100644 --- a/sdk/core/Azure.Core/src/Shared/ClientDiagnostics.cs +++ b/sdk/core/Azure.Core/src/Shared/ClientDiagnostics.cs @@ -62,13 +62,14 @@ internal static HttpMessageSanitizer CreateMessageSanitizer(DiagnosticsOptions d internal static string? GetResourceProviderNamespace(Assembly assembly) { - foreach (var customAttribute in assembly.GetCustomAttributes(true)) + foreach (var customAttribute in assembly.GetCustomAttributesData()) { // Weak bind internal shared type - var attributeType = customAttribute.GetType(); - if (attributeType.Name == "AzureResourceProviderNamespaceAttribute") + Type attributeType = customAttribute.AttributeType!; + if (attributeType.FullName == ("Azure.Core.AzureResourceProviderNamespaceAttribute")) { - return attributeType.GetProperty("ResourceProviderNamespace")?.GetValue(customAttribute) as string; + IList namedArguments = customAttribute.ConstructorArguments; + return namedArguments.Single().Value as string; } } diff --git a/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs b/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs index f88aec79d956..e3c4f23547df 100644 --- a/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs +++ b/sdk/core/Azure.Core/src/Shared/DiagnosticScope.cs @@ -24,6 +24,7 @@ namespace Azure.Core.Pipeline private readonly ActivityAdapter? _activityAdapter; private readonly bool _suppressNestedClientActivities; + [RequiresUnreferencedCode("The diagnosticSourceArgs are used in a call to DiagnosticSource.Write, all necessary properties need to be preserved on the type being passed in using DynamicDependency attributes.")] #if NETCOREAPP2_1 internal DiagnosticScope(string scopeName, DiagnosticListener source, object? diagnosticSourceArgs, object? activitySource, ActivityKind kind, bool suppressNestedClientActivities) #else @@ -136,6 +137,9 @@ public void Dispose() /// Marks the scope as failed. /// /// The exception to associate with the failed scope. + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "The Exception being passed into this method has public properties preserved on the inner method MarkFailed." + + "The public property System.Exception.TargetSite.get is not compatible with trimming and produces a warning when preserving all public properties. Since we do not use this property, and" + + "neither does Application Insights, we can suppress the warning coming from the inner method.")] public void Failed(Exception? exception = default) { _activityAdapter?.MarkFailed(exception); @@ -334,6 +338,8 @@ public void AddLink(string traceparent, string? tracestate, IDictionary(T exception) { if (exception != null) { @@ -500,7 +513,7 @@ public void MarkFailed(Exception? exception) _currentActivity?.SetErrorStatus(exception?.ToString()); } #endif -#if NET6_0_OR_GREATER +#if NET6_0_OR_GREATER // SetStatus is only defined in NET 6 or greater _currentActivity?.SetStatus(ActivityStatusCode.Error, exception?.ToString()); #endif } @@ -515,6 +528,7 @@ public void SetTraceContext(string traceparent, string? tracestate) _tracestate = tracestate; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The class constructor is marked with RequiresUnreferencedCode.")] public void Dispose() { var activity = _currentActivity ?? _sampleOutActivity; diff --git a/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs b/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs index f41d0d555fa2..6e3e7e7e0c57 100644 --- a/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs +++ b/sdk/core/Azure.Core/src/Shared/DiagnosticScopeFactory.cs @@ -5,6 +5,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Threading; #nullable enable @@ -49,6 +50,7 @@ public DiagnosticScopeFactory(string clientNamespace, string? resourceProviderNa public bool IsActivityEnabled { get; } + [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The DiagnosticScope constructor is marked as RequiresUnreferencedCode because of the usage of the diagnosticSourceArgs parameter. Since we are passing in null here we can suppress this warning.")] #if NETCOREAPP2_1 public DiagnosticScope CreateScope(string name, DiagnosticScope.ActivityKind kind = DiagnosticScope.ActivityKind.Internal) #else diff --git a/sdk/core/Azure.Core/tests/DynamicData/MutableJsonElementTests.cs b/sdk/core/Azure.Core/tests/DynamicData/MutableJsonElementTests.cs index 4f91487e4ede..5d0802e1fe5d 100644 --- a/sdk/core/Azure.Core/tests/DynamicData/MutableJsonElementTests.cs +++ b/sdk/core/Azure.Core/tests/DynamicData/MutableJsonElementTests.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Text.Json; using Azure.Core.Json; using NUnit.Framework; @@ -548,6 +549,36 @@ public void CanGetDateTimeOffset() Assert.Throws(() => mdoc.RootElement.GetProperty("foo").GetDateTimeOffset()); } + [Test] + public void StaticMethodsAreSameAsExpected() + { + var expectedMethods = new List { "GetString", "GetFormatExceptionText", "SerializeToJsonElement", "ParseFromBytes", "GetReaderForElement", "GetFirstSegment", "GetLastSegment", "CopyTo" }; + var actualMethods = Type.GetType("Azure.Core.Json.MutableJsonElement, Azure.Core").GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public); + + var message = $"There are {expectedMethods.Count} static methods expected in MutableJsonElement. If adding a new static method, ensure it is compatible with trimming or is annotated correctly."; + + foreach (var method in actualMethods) + { + var isExpected = expectedMethods.Contains(method.Name); + Assert.IsTrue(isExpected, message); + } + } + + [Test] + public void NestedTypesAreSameAsExpected() + { + var expectedNestedTypes = new List { "MutableJsonElementConverter", "ArrayEnumerator", "ObjectEnumerator" }; + var actualNestedTypes = Type.GetType("Azure.Core.Json.MutableJsonElement, Azure.Core").GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + var message = $"There are {expectedNestedTypes.Count} nested types expected in MutableJsonElement. If adding a new nested type, ensure it is compatible with trimming or is annotated correctly."; + + foreach (var nestedType in actualNestedTypes) + { + var isExpected = expectedNestedTypes.Contains(nestedType.Name); + Assert.IsTrue(isExpected, message); + } + } + #region Helpers internal static void ValidateToString(string json, MutableJsonElement element) diff --git a/sdk/core/Microsoft.Azure.Core.NewtonsoftJson/src/Microsoft.Azure.Core.NewtonsoftJson.csproj b/sdk/core/Microsoft.Azure.Core.NewtonsoftJson/src/Microsoft.Azure.Core.NewtonsoftJson.csproj index dfebc6ccacbd..d53458c4c293 100644 --- a/sdk/core/Microsoft.Azure.Core.NewtonsoftJson/src/Microsoft.Azure.Core.NewtonsoftJson.csproj +++ b/sdk/core/Microsoft.Azure.Core.NewtonsoftJson/src/Microsoft.Azure.Core.NewtonsoftJson.csproj @@ -19,6 +19,7 @@ + diff --git a/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/src/Microsoft.Azure.Core.Spatial.NewtonsoftJson.csproj b/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/src/Microsoft.Azure.Core.Spatial.NewtonsoftJson.csproj index ce707dade1cc..359524b48ce7 100644 --- a/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/src/Microsoft.Azure.Core.Spatial.NewtonsoftJson.csproj +++ b/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/src/Microsoft.Azure.Core.Spatial.NewtonsoftJson.csproj @@ -18,5 +18,6 @@ + diff --git a/sdk/core/Microsoft.Azure.Core.Spatial/src/Microsoft.Azure.Core.Spatial.csproj b/sdk/core/Microsoft.Azure.Core.Spatial/src/Microsoft.Azure.Core.Spatial.csproj index d588b90a244f..cf496637d6c9 100644 --- a/sdk/core/Microsoft.Azure.Core.Spatial/src/Microsoft.Azure.Core.Spatial.csproj +++ b/sdk/core/Microsoft.Azure.Core.Spatial/src/Microsoft.Azure.Core.Spatial.csproj @@ -20,5 +20,6 @@ + diff --git a/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/src/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.csproj b/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/src/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.csproj index 69b995a382df..fed8969766c9 100644 --- a/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/src/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.csproj +++ b/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/src/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.csproj @@ -14,6 +14,7 @@ + diff --git a/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/tests/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.Tests.csproj b/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/tests/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.Tests.csproj index 4cef4fa83692..f25a709f4419 100644 --- a/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/tests/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.Tests.csproj +++ b/sdk/eventgrid/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents/tests/Microsoft.Azure.Messaging.EventGrid.CloudNativeCloudEvents.Tests.csproj @@ -17,6 +17,7 @@ + diff --git a/sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/Microsoft.Azure.WebJobs.Extensions.EventGrid.csproj b/sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/Microsoft.Azure.WebJobs.Extensions.EventGrid.csproj index 83d827341337..8b8610999ecd 100644 --- a/sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/Microsoft.Azure.WebJobs.Extensions.EventGrid.csproj +++ b/sdk/eventgrid/Microsoft.Azure.WebJobs.Extensions.EventGrid/src/Microsoft.Azure.WebJobs.Extensions.EventGrid.csproj @@ -20,6 +20,7 @@ + diff --git a/sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Azure.Messaging.EventHubs.Processor.csproj b/sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Azure.Messaging.EventHubs.Processor.csproj index 5a0965221a14..cbde31a41806 100644 --- a/sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Azure.Messaging.EventHubs.Processor.csproj +++ b/sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Azure.Messaging.EventHubs.Processor.csproj @@ -37,6 +37,7 @@ + diff --git a/sdk/eventhub/Azure.Messaging.EventHubs.Shared/tests/Azure.Messaging.EventHubs.Shared.Tests.csproj b/sdk/eventhub/Azure.Messaging.EventHubs.Shared/tests/Azure.Messaging.EventHubs.Shared.Tests.csproj index 84fab443c812..a77178b5d6a6 100644 --- a/sdk/eventhub/Azure.Messaging.EventHubs.Shared/tests/Azure.Messaging.EventHubs.Shared.Tests.csproj +++ b/sdk/eventhub/Azure.Messaging.EventHubs.Shared/tests/Azure.Messaging.EventHubs.Shared.Tests.csproj @@ -39,6 +39,7 @@ + diff --git a/sdk/eventhub/Azure.Messaging.EventHubs/src/Azure.Messaging.EventHubs.csproj b/sdk/eventhub/Azure.Messaging.EventHubs/src/Azure.Messaging.EventHubs.csproj index a95daecef2b1..4cff1a05e6f0 100644 --- a/sdk/eventhub/Azure.Messaging.EventHubs/src/Azure.Messaging.EventHubs.csproj +++ b/sdk/eventhub/Azure.Messaging.EventHubs/src/Azure.Messaging.EventHubs.csproj @@ -38,6 +38,7 @@ + diff --git a/sdk/identity/Azure.Identity/perf/Azure.Identity.Perf.csproj b/sdk/identity/Azure.Identity/perf/Azure.Identity.Perf.csproj index 26f34323ed5c..c971a20ba5fe 100644 --- a/sdk/identity/Azure.Identity/perf/Azure.Identity.Perf.csproj +++ b/sdk/identity/Azure.Identity/perf/Azure.Identity.Perf.csproj @@ -19,6 +19,7 @@ + diff --git a/sdk/identity/Azure.Identity/src/Azure.Identity.csproj b/sdk/identity/Azure.Identity/src/Azure.Identity.csproj index 9cc6818e2014..571b0059e331 100644 --- a/sdk/identity/Azure.Identity/src/Azure.Identity.csproj +++ b/sdk/identity/Azure.Identity/src/Azure.Identity.csproj @@ -30,6 +30,7 @@ + diff --git a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj index 7d67fcf90ad7..80cc7efc0aee 100644 --- a/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj +++ b/sdk/monitor/Azure.Monitor.OpenTelemetry.AspNetCore/src/Azure.Monitor.OpenTelemetry.AspNetCore.csproj @@ -27,9 +27,5 @@ - - - - diff --git a/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro/src/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro.csproj b/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro/src/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro.csproj index b11d2d8c0836..c906835103d8 100644 --- a/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro/src/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro.csproj +++ b/sdk/schemaregistry/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro/src/Microsoft.Azure.Data.SchemaRegistry.ApacheAvro.csproj @@ -26,6 +26,7 @@ + diff --git a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj index 3cc6e988c0ba..30f4a4876210 100644 --- a/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj +++ b/sdk/servicebus/Azure.Messaging.ServiceBus/src/Azure.Messaging.ServiceBus.csproj @@ -35,6 +35,7 @@ + diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj index b64f6c379bef..678e2936978a 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj @@ -32,6 +32,7 @@ + diff --git a/sdk/storage/Azure.Storage.Blobs.ChangeFeed/src/Azure.Storage.Blobs.ChangeFeed.csproj b/sdk/storage/Azure.Storage.Blobs.ChangeFeed/src/Azure.Storage.Blobs.ChangeFeed.csproj index 5e3bed907c2f..fa7e4ba4e3ed 100644 --- a/sdk/storage/Azure.Storage.Blobs.ChangeFeed/src/Azure.Storage.Blobs.ChangeFeed.csproj +++ b/sdk/storage/Azure.Storage.Blobs.ChangeFeed/src/Azure.Storage.Blobs.ChangeFeed.csproj @@ -27,6 +27,7 @@ +