Elastic.Apm.StackExchange.Redis for IDistributedCache?

Hello,

in our project, we use the Microsoft.Extensions.Caching.Distributed package for configuring the Redis cache like shown in this code snippet:

services.AddStackExchangeRedisCache(options =>
{
  options.Configuration = "redis.local:6379";
  options.InstanceName = "XF";
});

Is there any possibility to get it working with Elastic.Apm.StackExchange.Redis? I tried several things but was not able to get it work.

Best regards,
Thorsten

Hi @ThorstenKraus,

Elastic.Apm.StackExchange.Redis targets the StackExchange.Redis package - so if you use redis via StackExchange.Redis, then I it should work. Your code snippet does not tell much, but based on services.AddStackExchangeRedisCache I suspect you use this package.

You need to have access to the IConnectionMultiplexer from Redis and call .UseElasticApm on it (that method is in the Elastic.Apm.StackExchange.Redis package.

Docs here: StackExchange.Redis | APM .NET Agent Reference [1.x] | Elastic

Do you have access to IConnectionMultiplexer over the options variable? If so, then just call the method - if you don't, then I suggest the approach should be that you try to figure out how you can have access to the IConnectionMultiplexer so you can register the APM Agent with it.

Hi @GregKalapos ,

thank you very much for your answer!

As far as I see I don't have access to the IConnectionMulitplexer over the options variable. But it seems to be possible to access an property called 'ConnectionMultiplexerFactory' over the options variable. I tried to use this but unfortunately with no success. The first line of code results in a NullReferenceException.

IConnectionMultiplexer connectionMultiplexer = ConnectionMultiplexer.Connect("localhost:6379");
                services.AddSingleton(connectionMultiplexer);
                services.AddStackExchangeRedisCache(options =>
                {
                    options.ConnectionMultiplexerFactory = () => Task.FromResult(connectionMultiplexer);
                });

Do you have any idea how to get this working? It would be wonderful if you would have an code snippet for me so that I can have a look at it and try to adapt my code. Hint: I have some business logic services which expect an IDistributedCache (from Microsoft.Extensions.Caching.Distributed) in their constructor and they get this via dependency injection - this way is needed also in future.

Best regards,
Thorsten

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.