diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 466f2061b..f87f6fd2a 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -10,6 +10,7 @@ Current package versions: - Fix [#2426](https://github.com/StackExchange/StackExchange.Redis/issues/2426): Don't restrict multi-slot operations on Envoy proxy; let the proxy decide ([#2428 by mgravell](https://github.com/StackExchange/StackExchange.Redis/pull/2428)) - Add: Support for `User`/`Password` in `DefaultOptionsProvider` to support token rotation scenarios ([#2445 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2445)) +- Fix [#2449](https://github.com/StackExchange/StackExchange.Redis/issues/2449): Resolve AOT trim warnings in `TryGetAzureRoleInstanceIdNoThrow` ([#2451 by eerhardt](https://github.com/StackExchange/StackExchange.Redis/pull/2451)) ## 2.6.104 diff --git a/src/StackExchange.Redis/ChannelMessageQueue.cs b/src/StackExchange.Redis/ChannelMessageQueue.cs index 435b067fc..3cc6c3d5a 100644 --- a/src/StackExchange.Redis/ChannelMessageQueue.cs +++ b/src/StackExchange.Redis/ChannelMessageQueue.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Reflection; -using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; @@ -126,6 +125,7 @@ public ValueTask ReadAsync(CancellationToken cancellationToken = /// The (approximate) count of items in the Channel. public bool TryGetCount(out int count) { +#if NETCOREAPP3_1 // get this using the reflection try { @@ -137,6 +137,15 @@ public bool TryGetCount(out int count) } } catch { } +#else + var reader = _queue.Reader; + if (reader.CanCount) + { + count = reader.Count; + return true; + } +#endif + count = default; return false; } diff --git a/src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs b/src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs index f2fd42757..f990d8265 100644 --- a/src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs +++ b/src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs @@ -225,31 +225,21 @@ protected virtual string GetDefaultClientName() => string? roleInstanceId; try { - Assembly? asm = null; - foreach (var asmb in AppDomain.CurrentDomain.GetAssemblies()) - { - if (asmb.GetName()?.Name?.Equals("Microsoft.WindowsAzure.ServiceRuntime") == true) - { - asm = asmb; - break; - } - } - if (asm == null) - return null; - - var type = asm.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment"); + var roleEnvironmentType = Type.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, Microsoft.WindowsAzure.ServiceRuntime", throwOnError: false); // https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.isavailable.aspx - if (type?.GetProperty("IsAvailable") is not PropertyInfo isAvailableProp + if (roleEnvironmentType?.GetProperty("IsAvailable") is not PropertyInfo isAvailableProp || isAvailableProp.GetValue(null, null) is not bool isAvailableVal || !isAvailableVal) { return null; } - var currentRoleInstanceProp = type.GetProperty("CurrentRoleInstance"); + var currentRoleInstanceProp = roleEnvironmentType.GetProperty("CurrentRoleInstance"); var currentRoleInstanceId = currentRoleInstanceProp?.GetValue(null, null); - roleInstanceId = currentRoleInstanceId?.GetType().GetProperty("Id")?.GetValue(currentRoleInstanceId, null)?.ToString(); + + var roleInstanceType = Type.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleInstance, Microsoft.WindowsAzure.ServiceRuntime", throwOnError: false); + roleInstanceId = roleInstanceType?.GetProperty("Id")?.GetValue(currentRoleInstanceId, null)?.ToString(); if (roleInstanceId.IsNullOrEmpty()) {