# Elastic.Apm.StackExchange.Redis for IDistributedCache?

**URL:** <https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [March 27, 2023, 7:56am UTC](https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581 "2023-03-27T07:56:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThorstenKraus](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@ThorstenKraus](https://discuss.elastic.co/u/ThorstenKraus)\
**Post date:** [March 27, 2023, 7:56am UTC](https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581/1 "2023-03-27T07:56:21Z")

</div>

Hello,

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

```auto
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

---

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [April 8, 2023, 10:05am UTC](https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581/2 "2023-04-08T10:05:17Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/dotnet/current/setup-stackexchange-redis.html)

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.

---

<div class="post-metadata">

**Author:** ![ThorstenKraus](https://avatars.discourse-cdn.com/v4/letter/t/eb9ed0/32.png) [@ThorstenKraus](https://discuss.elastic.co/u/ThorstenKraus)\
**Post date:** [April 17, 2023, 9:42am UTC](https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581/3 "2023-04-17T09:42:04Z")

</div>

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.

```auto
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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 8, 2023, 5:42am UTC](https://discuss.elastic.co/t/elastic-apm-stackexchange-redis-for-idistributedcache/328581/4 "2023-05-08T05:42:47Z")

</div>

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