# Why static API for .net core agent?

**URL:** <https://discuss.elastic.co/t/why-static-api-for-net-core-agent/236465>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [June 10, 2020, 8:56am UTC](https://discuss.elastic.co/t/why-static-api-for-net-core-agent/236465 "2020-06-10T08:56:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcin\_Druzgala](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marcin_druzgala/32/45030_2.png) [@Marcin\_Druzgala](https://discuss.elastic.co/u/Marcin_Druzgala)\
**Post date:** [June 10, 2020, 8:56am UTC](https://discuss.elastic.co/t/why-static-api-for-net-core-agent/236465/1 "2020-06-10T08:56:33Z")

</div>

**APM Server version** : 1.5.1

**APM Agent language and version** : dotnet

Hi,

We started working with APM agent for .net core (Elastic.Apm.NetCoreAll) and it works nice but I have couple of questions:

Why is _everything_ static? It feels really awkward to use Singletons inside .net core app in 2020 - especially since .net core was build _on top_ of IoC/DI.

It also makes it super hard to unit test code that uses `Agent.Tracer.CaptureTransaction` or `Agent.Tracer.CaptureSpan` (it is almost impossible to be honest - we have to refactor code to use the hack [Mock an APM instance](https://discuss.elastic.co/t/mock-an-apm-instance/224686/2)).

How will that singleton behave if we are using the APM tracing not only for http requests but also for rabbitmq events - since everything is a singleton is there a chance that by accessing agent singleton instance while we have both rabbitmq and http in progress might be problematic?

Thanks in advance! And keep up the great work 🙂

---

<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:** [June 10, 2020, 9:25am UTC](https://discuss.elastic.co/t/why-static-api-for-net-core-agent/236465/2 "2020-06-10T09:25:20Z")

</div>

Hi @Marcin_Druzgala,

I wrote about why we need a singleton [here](https://github.com/elastic/apm-agent-dotnet/issues/824#issuecomment-617867168). If we let users new up tons of agent instances then it'd open up lots of other issues.

> It also makes it super hard to unit test code that uses `Agent.Tracer.CaptureTransaction` or `Agent.Tracer.CaptureSpan` (it is almost impossible to be honest - we have to refactor code to use the hack [Mock an APM instance](https://discuss.elastic.co/t/mock-an-apm-instance/224686/2)).

One suggestion I can give you is that you could just inject the `ITracer` instance (that's the `Agent.Tracer`) into the classes you test and in a test environment just set it to `null` and use it with `?.` in your code. That way if it's not prod and the agent is not configured there won't be traces and the API won't do anything.

> How will that singleton behave if we are using the APM tracing not only for http requests but also for rabbitmq events - since everything is a singleton is there a chance that by accessing agent singleton instance while we have both rabbitmq and http in progress might be problematic?

I don't think it'd be problematic, in fact in order to correlate all the spans we can't have independent agent instances. If you'd have 2 independent agent instances for the 2 libraries it'd be even harder to correlate them.

Now, having said that - there are already some issues in the agent repo around this topic to make some improvements. [This one](https://github.com/elastic/apm-agent-dotnet/issues/458) is an example.

---

<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:** [July 1, 2020, 5:25am UTC](https://discuss.elastic.co/t/why-static-api-for-net-core-agent/236465/3 "2020-07-01T05:25:26Z")

</div>

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