Skip to content

Commit d487d0b

Browse files
committed
Support AOT
1 parent 7d82888 commit d487d0b

File tree

3 files changed

+53
-17
lines changed

3 files changed

+53
-17
lines changed

src/Confluent.Kafka/Impl/LibRdKafka.cs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#if NET462
3030
using System.ComponentModel;
3131
#endif
32+
#if NET5_0_OR_GREATER
33+
using System.Diagnostics.CodeAnalysis;
34+
#endif
3235

3336

3437
namespace Confluent.Kafka.Impl
@@ -173,7 +176,11 @@ public static string LastError
173176
}
174177
}
175178

176-
static bool SetDelegates(Type nativeMethodsClass)
179+
static bool SetDelegates(
180+
#if NET5_0_OR_GREATER
181+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
182+
#endif
183+
Type nativeMethodsClass)
177184
{
178185
var methods = nativeMethodsClass.GetRuntimeMethods().ToArray();
179186

@@ -673,14 +680,37 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)
673680

674681
#endif
675682

676-
private static bool TrySetDelegates(List<Type> nativeMethodCandidateTypes)
683+
private static bool TrySetDelegates(
684+
#if NET5_0_OR_GREATER
685+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
686+
#endif
687+
Type nativeMethodCandidateType)
677688
{
678-
foreach (var t in nativeMethodCandidateTypes)
689+
if (SetDelegates(nativeMethodCandidateType))
679690
{
680-
if (SetDelegates(t))
681-
{
682-
return true;
683-
}
691+
return true;
692+
}
693+
694+
throw new DllNotFoundException("Failed to load the librdkafka native library.");
695+
}
696+
697+
private static bool TrySetDelegates(
698+
#if NET5_0_OR_GREATER
699+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
700+
#endif
701+
Type nativeMethodCandidateType1,
702+
#if NET5_0_OR_GREATER
703+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
704+
#endif
705+
Type nativeMethodCandidateType2)
706+
{
707+
if (SetDelegates(nativeMethodCandidateType1))
708+
{
709+
return true;
710+
}
711+
if (SetDelegates(nativeMethodCandidateType2))
712+
{
713+
return true;
684714
}
685715

686716
throw new DllNotFoundException("Failed to load the librdkafka native library.");
@@ -698,7 +728,7 @@ private static void LoadNetStandardDelegates(string userSpecifiedPath)
698728
}
699729
}
700730

701-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
731+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
702732
}
703733

704734
private static void LoadOSXDelegates(string userSpecifiedPath)
@@ -711,7 +741,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
711741
}
712742
}
713743

714-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
744+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
715745
}
716746

717747
private static void LoadLinuxDelegates(string userSpecifiedPath)
@@ -723,7 +753,7 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
723753
throw new InvalidOperationException($"Failed to load librdkafka at location '{userSpecifiedPath}'. dlerror: '{PosixNative.LastError}'.");
724754
}
725755

726-
TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
756+
TrySetDelegates(typeof(NativeMethods.NativeMethods));
727757
}
728758
else
729759
{
@@ -732,17 +762,16 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
732762
var osName = PlatformApis.GetOSName();
733763
if (osName.Equals("alpine", StringComparison.OrdinalIgnoreCase))
734764
{
735-
delegates.Add(typeof(NativeMethods.NativeMethods_Alpine));
765+
TrySetDelegates(typeof(NativeMethods.NativeMethods_Alpine));
736766
}
737767
else
738768
{
739769
// Try to load first the shared library with GSSAPI linkage
740770
// and then the one without.
741-
delegates.Add(typeof(NativeMethods.NativeMethods));
742-
delegates.Add(typeof(NativeMethods.NativeMethods_Centos8));
771+
TrySetDelegates(
772+
typeof(NativeMethods.NativeMethods),
773+
typeof(NativeMethods.NativeMethods_Centos8));
743774
}
744-
745-
TrySetDelegates(delegates);
746775
}
747776
}
748777

src/Confluent.Kafka/Impl/SafeKafkaHandle.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ internal void DeleteConsumerGroupOffsets(String group, IEnumerable<TopicPartitio
18191819
setOption_OperationTimeout(optionsPtr, options.OperationTimeout);
18201820
setOption_completionSource(optionsPtr, completionSourcePtr);
18211821

1822-
if (partitions.Where(tp => tp.Topic == null || tp.Partition == null).Count() > 0)
1822+
if (partitions.Where(tp => tp.Topic == null).Count() > 0)
18231823
{
18241824
throw new ArgumentException("Cannot delete offsets because one or more topics or partitions were specified as null.");
18251825
}

src/Confluent.Kafka/Internal/Util.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using SystemMarshal = System.Runtime.InteropServices.Marshal;
2020
using SystemGCHandle = System.Runtime.InteropServices.GCHandle;
2121
using SystemGCHandleType = System.Runtime.InteropServices.GCHandleType;
22+
#if NET5_0_OR_GREATER
23+
using System.Diagnostics.CodeAnalysis;
24+
#endif
2225

2326

2427
namespace Confluent.Kafka.Internal
@@ -79,7 +82,11 @@ public unsafe static string PtrToStringUTF8(IntPtr strPtr, UIntPtr strLength)
7982
return Encoding.UTF8.GetString((byte*)strPtr.ToPointer(), (int)strLength);
8083
}
8184

82-
public static T PtrToStructure<T>(IntPtr ptr)
85+
public static T PtrToStructure<
86+
#if NET5_0_OR_GREATER
87+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
88+
#endif
89+
T>(IntPtr ptr)
8390
{
8491
return SystemMarshal.PtrToStructure<T>(ptr);
8592
}

0 commit comments

Comments
 (0)