Skip to content

Commit 3ba8d2a

Browse files
authored
Fix first round of trim warnings (#2451)
- DefaultOptionsProvider.TryGetAzureRoleInstanceIdNoThrow use Type.GetType to check for Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, so the trimmer knows to preserve this type, if it is in the app - ChannelMessage use the added public API for ChannelReader CanCount and Count, but fallback to reflection on netcoreapp3.1, since those APIs are not available on that runtime. Contributes to #2449 This at least removes the warnings from using `Microsoft.Extensions.Caching.StackExchangeRedis`. We could also add a trimming test app as outlined in https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#show-all-warnings-with-sample-application, along with a unit test that publishes the app and ensures there are no new warnings. LMK if you think this is valuable. There are still these warnings left in this library: ``` /_/src/StackExchange.Redis/ScriptParameterMapper.cs(173): Trim analysis warning IL2070: StackExchange.Redis.ScriptParameterMapper.IsValidParameterHash(Type,LuaScript,String&,String&): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The parameter 't' of method 'StackExchange.Redis.ScriptParameterMapper.IsValidParameterHash(Type,LuaScript,String&,String&)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(227): Trim analysis warning IL2070: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyAccessedMemberTypes.PublicMethods', 'DynamicallyAccessedMemberTypes.PublicFields', 'DynamicallyAccessedMemberTypes.PublicNestedTypes', 'DynamicallyAccessedMemberTypes.PublicProperties', 'DynamicallyAccessedMemberTypes.PublicEvents' in call to 'System.Type.GetMember(String)'. The parameter 't' of method 'StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(260): Trim analysis warning IL2026: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): Using member 'System.Linq.Expressions.Expression.Property(Expression,String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] /_/src/StackExchange.Redis/ScriptParameterMapper.cs(261): Trim analysis warning IL2026: StackExchange.Redis.ScriptParameterMapper.GetParameterExtractor(Type,LuaScript): Using member 'System.Linq.Expressions.Expression.Call(Expression,String,Type[],Expression[])' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Creating Expressions requires unreferenced code because the members being referenced by the Expression may be trimmed. [C:\git\azure-activedirectory-identitymodel-extensions-for-dotnet\test\Microsoft.IdentityModel.AotCompatibility.TestApp\Microsoft.IdentityModel.AotCompatibility.TestApp.csproj] ``` Fixing those will require a bit more work, as the whole LuaScript functionality looks like it needs to be marked `RequiresUnreferencedCode`. cc @mgravell @NickCraver
1 parent 7ad0add commit 3ba8d2a

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

docs/ReleaseNotes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Current package versions:
1010

1111
- 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))
1212
- Add: Support for `User`/`Password` in `DefaultOptionsProvider` to support token rotation scenarios ([#2445 by NickCraver](https://github.com/StackExchange/StackExchange.Redis/pull/2445))
13+
- 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))
1314

1415
## 2.6.104
1516

src/StackExchange.Redis/ChannelMessageQueue.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Reflection;
4-
using System.Runtime.CompilerServices;
54
using System.Threading;
65
using System.Threading.Channels;
76
using System.Threading.Tasks;
@@ -126,6 +125,7 @@ public ValueTask<ChannelMessage> ReadAsync(CancellationToken cancellationToken =
126125
/// <param name="count">The (approximate) count of items in the Channel.</param>
127126
public bool TryGetCount(out int count)
128127
{
128+
#if NETCOREAPP3_1
129129
// get this using the reflection
130130
try
131131
{
@@ -137,6 +137,15 @@ public bool TryGetCount(out int count)
137137
}
138138
}
139139
catch { }
140+
#else
141+
var reader = _queue.Reader;
142+
if (reader.CanCount)
143+
{
144+
count = reader.Count;
145+
return true;
146+
}
147+
#endif
148+
140149
count = default;
141150
return false;
142151
}

src/StackExchange.Redis/Configuration/DefaultOptionsProvider.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -225,31 +225,21 @@ protected virtual string GetDefaultClientName() =>
225225
string? roleInstanceId;
226226
try
227227
{
228-
Assembly? asm = null;
229-
foreach (var asmb in AppDomain.CurrentDomain.GetAssemblies())
230-
{
231-
if (asmb.GetName()?.Name?.Equals("Microsoft.WindowsAzure.ServiceRuntime") == true)
232-
{
233-
asm = asmb;
234-
break;
235-
}
236-
}
237-
if (asm == null)
238-
return null;
239-
240-
var type = asm.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment");
228+
var roleEnvironmentType = Type.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment, Microsoft.WindowsAzure.ServiceRuntime", throwOnError: false);
241229

242230
// https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleenvironment.isavailable.aspx
243-
if (type?.GetProperty("IsAvailable") is not PropertyInfo isAvailableProp
231+
if (roleEnvironmentType?.GetProperty("IsAvailable") is not PropertyInfo isAvailableProp
244232
|| isAvailableProp.GetValue(null, null) is not bool isAvailableVal
245233
|| !isAvailableVal)
246234
{
247235
return null;
248236
}
249237

250-
var currentRoleInstanceProp = type.GetProperty("CurrentRoleInstance");
238+
var currentRoleInstanceProp = roleEnvironmentType.GetProperty("CurrentRoleInstance");
251239
var currentRoleInstanceId = currentRoleInstanceProp?.GetValue(null, null);
252-
roleInstanceId = currentRoleInstanceId?.GetType().GetProperty("Id")?.GetValue(currentRoleInstanceId, null)?.ToString();
240+
241+
var roleInstanceType = Type.GetType("Microsoft.WindowsAzure.ServiceRuntime.RoleInstance, Microsoft.WindowsAzure.ServiceRuntime", throwOnError: false);
242+
roleInstanceId = roleInstanceType?.GetProperty("Id")?.GetValue(currentRoleInstanceId, null)?.ToString();
253243

254244
if (roleInstanceId.IsNullOrEmpty())
255245
{

0 commit comments

Comments
 (0)