Skip to content

Commit 4a4a28c

Browse files
authored
Annotate Azure.Messaging.EventHubs for trim/AOT compat (#46606)
* Annotate Azure.Messaging.EventHubs for trim/AOT compat * PR feedback - Annotate Azure.Messaging.EventHubs.Processor, add ci check - Add changelog entries Also make some minor fixes to the AOT compat script. * Fix RequiresUnreferencedCode message
1 parent 8cba7d7 commit 4a4a28c

File tree

11 files changed

+87
-7
lines changed

11 files changed

+87
-7
lines changed

eng/scripts/compatibility/Check-AOT-Compatibility.ps1

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ if ($LASTEXITCODE -ne 0)
7272
Set-Location -Path ..
7373
Remove-Item -Path "./$folderPath" -Recurse -Force
7474

75-
Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
75+
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
7676
Exit 2
7777
}
7878

@@ -104,15 +104,18 @@ if (Test-Path $expectedWarningsFullPath -PathType Leaf) {
104104
$numWarnings = $warnings.Count
105105

106106
if ($numWarnings -gt 0) {
107-
Write-Host "Found $numWarnings additional warnings that were not expected:`n$warnings"
107+
Write-Host "Found $numWarnings additional warnings that were not expected:"
108+
foreach ($warning in $warnings) {
109+
Write-Host $warning
110+
}
108111
}
109112

110113
Write-Host "Deleting test app files."
111114

112115
Set-Location -Path ..
113116
Remove-Item -Path "./$folderPath" -Recurse -Force
114117

115-
Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
118+
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
116119

117120
exit $warnings.Count
118121
}
@@ -126,7 +129,10 @@ Write-Host "Checking against the list of expected warnings. There are $numExpect
126129
$warnings = $publishOutput -split "`n" | select-string -pattern 'IL\d+' | select-string -pattern '##' -notmatch | select-string -pattern $expectedWarnings -notmatch
127130
$numWarnings = $warnings.Count
128131
if ($numWarnings -gt 0) {
129-
Write-Host "Found $numWarnings additional warnings that were not expected:`n$warnings"
132+
Write-Host "Found $numWarnings additional warnings that were not expected:"
133+
foreach ($warning in $warnings) {
134+
Write-Host $warning
135+
}
130136
}
131137

132138
### Cleanup ###
@@ -138,9 +144,9 @@ Remove-Item -Path "./$folderPath" -Recurse -Force
138144

139145
if ($numExpectedWarnings -ne $actualWarningCount) {
140146
Write-Host "The number of expected warnings ($numExpectedWarnings) was different than the actual warning count ($actualWarningCount)."
141-
Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
147+
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
142148
exit 2
143149
}
144150

145-
Write-Host "\nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
151+
Write-Host "`nFor help with this check, please see https://github.com/Azure/azure-sdk-for-net/tree/main/doc/dev/AotRegressionChecks.md"
146152
exit $warnings.Count

sdk/core/Azure.Core/src/Shared/AzureEventSource.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
#nullable enable
@@ -12,6 +12,9 @@ namespace Azure.Core.Diagnostics
1212
{
1313
internal abstract class AzureEventSource: EventSource
1414
{
15+
private protected const string EventSourceRequiresUnreferencedCodeMessage = "EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type";
16+
private protected const string EventSourceSuppressMessage = "Parameters to this method are primitive and are trimmer safe.";
17+
1518
private const string SharedDataKey = "_AzureEventSourceNamesInUse";
1619
private static readonly HashSet<string> NamesInUse;
1720

sdk/eventhub/Azure.Messaging.EventHubs.Processor/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Other Changes
1212

13+
- Added annotations to make the package compatible with trimming and native AOT compilation.
14+
1315
## 5.11.5 (2024-08-14)
1416

1517
### Other Changes

sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Azure.Messaging.EventHubs.Processor.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<PackageTags>Azure;Event Hubs;EventHubs;.NET;Event Processor;EventProcessor;$(PackageCommonTags)</PackageTags>
88
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
11+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
1012
</PropertyGroup>
1113

1214
<ItemGroup>

sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Diagnostics/BlobEventStoreEventSource.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics.Tracing;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Runtime.CompilerServices;
78
using Azure.Core.Diagnostics;
89
using Azure.Messaging.EventHubs.Primitives;
@@ -91,6 +92,7 @@ public virtual void ListOwnershipStart(string fullyQualifiedNamespace,
9192
/// <param name="ownershipCount">The amount of ownership received from the storage service.</param>
9293
///
9394
[Event(22, Level = EventLevel.Verbose, Message = "Completed listing ownership for FullyQualifiedNamespace: '{0}'; EventHubName: '{1}'; ConsumerGroup: '{2}'. There were {3} ownership entries were found.")]
95+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
9496
public virtual void ListOwnershipComplete(string fullyQualifiedNamespace,
9597
string eventHubName,
9698
string consumerGroup,
@@ -434,6 +436,7 @@ public virtual void GetCheckpointError(string fullyQualifiedNamespace,
434436
///
435437
[NonEvent]
436438
[MethodImpl(MethodImplOptions.AggressiveInlining)]
439+
[RequiresUnreferencedCode(EventSourceRequiresUnreferencedCodeMessage)]
437440
private unsafe void WriteEvent<TValue1>(int eventId,
438441
string arg1,
439442
string arg2,
@@ -476,6 +479,7 @@ private unsafe void WriteEvent<TValue1>(int eventId,
476479
///
477480
[NonEvent]
478481
[MethodImpl(MethodImplOptions.AggressiveInlining)]
482+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
479483
private unsafe void WriteEvent(int eventId,
480484
string arg1,
481485
string arg2,
@@ -518,6 +522,7 @@ private unsafe void WriteEvent(int eventId,
518522
///
519523
[NonEvent]
520524
[MethodImpl(MethodImplOptions.AggressiveInlining)]
525+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
521526
private unsafe void WriteEvent(int eventId,
522527
string arg1,
523528
string arg2,
@@ -567,6 +572,7 @@ private unsafe void WriteEvent(int eventId,
567572
///
568573
[NonEvent]
569574
[MethodImpl(MethodImplOptions.AggressiveInlining)]
575+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
570576
private unsafe void WriteEvent(int eventId,
571577
string arg1,
572578
string arg2,
@@ -621,6 +627,7 @@ private unsafe void WriteEvent(int eventId,
621627
///
622628
[NonEvent]
623629
[MethodImpl(MethodImplOptions.AggressiveInlining)]
630+
[RequiresUnreferencedCode(EventSourceRequiresUnreferencedCodeMessage)]
624631
private unsafe void WriteEvent<TValue1>(int eventId,
625632
string arg1,
626633
string arg2,
@@ -677,6 +684,7 @@ private unsafe void WriteEvent<TValue1>(int eventId,
677684
///
678685
[NonEvent]
679686
[MethodImpl(MethodImplOptions.AggressiveInlining)]
687+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
680688
private unsafe void WriteEvent(int eventId,
681689
string arg1,
682690
string arg2,
@@ -744,6 +752,7 @@ private unsafe void WriteEvent(int eventId,
744752
///
745753
[NonEvent]
746754
[MethodImpl(MethodImplOptions.AggressiveInlining)]
755+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
747756
private unsafe void WriteEvent(int eventId,
748757
string arg1,
749758
string arg2,

sdk/eventhub/Azure.Messaging.EventHubs.Processor/src/Diagnostics/EventProcessorClientEventSource.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Diagnostics.Tracing;
67
using System.Runtime.CompilerServices;
78
using Azure.Core.Diagnostics;
@@ -235,6 +236,7 @@ public virtual void EventBatchProcessingHandlerCall(string sequenceNumber,
235236
///
236237
[NonEvent]
237238
[MethodImpl(MethodImplOptions.AggressiveInlining)]
239+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
238240
private unsafe void WriteEvent(int eventId,
239241
string arg1,
240242
string arg2,
@@ -278,6 +280,7 @@ private unsafe void WriteEvent(int eventId,
278280
///
279281
[NonEvent]
280282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
283+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
281284
private unsafe void WriteEvent(int eventId,
282285
string arg1,
283286
string arg2,
@@ -327,6 +330,7 @@ private unsafe void WriteEvent(int eventId,
327330
///
328331
[NonEvent]
329332
[MethodImpl(MethodImplOptions.AggressiveInlining)]
333+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = EventSourceSuppressMessage)]
330334
private unsafe void WriteEvent(int eventId,
331335
string arg1,
332336
string arg2,

sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
### Other Changes
1414

15+
- Added annotations to make the package compatible with trimming and native AOT compilation.
16+
1517
## 5.11.5 (2024-07-31)
1618

1719
### Other Changes

sdk/eventhub/Azure.Messaging.EventHubs/src/Azure.Messaging.EventHubs.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
<PackageTags>Azure;Event Hubs;EventHubs;.NET;AMQP;IoT;$(PackageCommonTags)</PackageTags>
88
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
11+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
1012
</PropertyGroup>
1113

1214
<ItemGroup>

sdk/eventhub/Azure.Messaging.EventHubs/src/Compatibility/EventHubClientBuilderExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using Azure.Core.Extensions;
56
using Azure.Messaging.EventHubs.Consumer;
67
using Azure.Messaging.EventHubs.Producer;
@@ -46,6 +47,8 @@ public static IAzureClientBuilder<EventHubProducerClient, EventHubProducerClient
4647
/// Registers a <see cref="EventHubProducerClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance.
4748
/// </summary>
4849
///
50+
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
51+
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
4952
public static IAzureClientBuilder<EventHubProducerClient, EventHubProducerClientOptions> AddEventHubProducerClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration)
5053
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
5154
{
@@ -86,6 +89,8 @@ public static IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClient
8689
/// Registers a <see cref="EventHubConsumerClient"/> instance with connection options loaded from the provided <paramref name="configuration"/> instance.
8790
/// </summary>
8891
///
92+
[RequiresUnreferencedCode("Binding strongly typed objects to configuration values is not supported with trimming. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
93+
[RequiresDynamicCode("Binding strongly typed objects to configuration values requires generating dynamic code at runtime, for example instantiating generic types. Use the Configuration Binder Source Generator (EnableConfigurationBindingGenerator=true) instead.")]
8994
public static IAzureClientBuilder<EventHubConsumerClient, EventHubConsumerClientOptions> AddEventHubConsumerClient<TBuilder, TConfiguration>(this TBuilder builder, TConfiguration configuration)
9095
where TBuilder : IAzureClientFactoryBuilderWithConfiguration<TConfiguration>
9196
{

0 commit comments

Comments
 (0)