1- using System . Reflection ;
1+ using System . Diagnostics ;
2+ using System . Reflection ;
23using DistributedCache . Dtos ;
34using DistributedCache . Enums ;
45using DistributedCache . Helpers ;
@@ -17,15 +18,14 @@ public class RedisRateLimitService(
1718 private readonly IRedisDatabase _redisDatabase = redisClient . GetDefaultDatabase ( ) ;
1819 private readonly CacheConfigurationOptions _config = options . Value ;
1920
20- private readonly string _moduleName = Assembly . GetCallingAssembly ( )
21- . GetName ( ) . Name ! ;
22-
2321 public async ValueTask < RateLimitState > RateLimitAsync ( RateLimitConfiguration rateLimitConfiguration ,
2422 CancellationToken cancellationToken = default )
2523 {
24+ var assemblyName = GetCallingAssemblyName ( ) ;
25+
2626 var key = _config . KeyPrefixForIsolation == KeyPrefix . None
2727 ? KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) )
28- : KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) , _moduleName ) ;
28+ : KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) , assemblyName ) ;
2929
3030
3131 var lockValue = Guid . NewGuid ( )
@@ -94,4 +94,18 @@ public async ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rat
9494 await lockService . ReleaseLockAsync ( key , lockValue ) ;
9595 }
9696 }
97+
98+ private static string GetCallingAssemblyName ( )
99+ {
100+ var stackTrace = new StackTrace ( ) ;
101+ foreach ( var frame in stackTrace . GetFrames ( ) )
102+ {
103+ var method = frame . GetMethod ( ) ;
104+ if ( method != null && method . DeclaringType != typeof ( RedisRateLimitService ) )
105+ {
106+ return method . DeclaringType ! . Assembly . GetName ( ) . Name ! ;
107+ }
108+ }
109+ return Assembly . GetExecutingAssembly ( ) . GetName ( ) . Name ! ;
110+ }
97111}
0 commit comments