1
- using System . Reflection ;
1
+ using System . Diagnostics ;
2
+ using System . Reflection ;
2
3
using DistributedCache . Dtos ;
3
4
using DistributedCache . Enums ;
4
5
using DistributedCache . Helpers ;
@@ -17,15 +18,14 @@ public class RedisRateLimitService(
17
18
private readonly IRedisDatabase _redisDatabase = redisClient . GetDefaultDatabase ( ) ;
18
19
private readonly CacheConfigurationOptions _config = options . Value ;
19
20
20
- private readonly string _moduleName = Assembly . GetCallingAssembly ( )
21
- . GetName ( ) . Name ! ;
22
-
23
21
public async ValueTask < RateLimitState > RateLimitAsync ( RateLimitConfiguration rateLimitConfiguration ,
24
22
CancellationToken cancellationToken = default )
25
23
{
24
+ var assemblyName = GetCallingAssemblyName ( ) ;
25
+
26
26
var key = _config . KeyPrefixForIsolation == KeyPrefix . None
27
27
? KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) )
28
- : KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) , _moduleName ) ;
28
+ : KeyFormatHelper . GetPrefixedKey ( rateLimitConfiguration . GetKey ( ) , assemblyName ) ;
29
29
30
30
31
31
var lockValue = Guid . NewGuid ( )
@@ -94,4 +94,18 @@ public async ValueTask<RateLimitState> RateLimitAsync(RateLimitConfiguration rat
94
94
await lockService . ReleaseLockAsync ( key , lockValue ) ;
95
95
}
96
96
}
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
+ }
97
111
}
0 commit comments