Application Insights for .NET isn't compatible with .NET feature "keyed (named) services"

.NET has a feature called keyed (or named) services, which allows you to register the same service (with different configurations or implementations, for example) under different names and then inject it using the specified name. This feature has quickly become widespread in some .NET projects, such as Semantic Kernel — a library providing abstractions for AI.

However, it turns out that Application Insights has a bug caused by a service registration process incompatible with keyed services. This bug results in a very difficult-to-diagnose error, which can sometimes occur randomly.

As a workaround, you can:

  • Avoid using keyed services.
  • Add Application Insights as an optional dependency to your module, ensuring it loads before any keyed service registrations, thereby preventing the error:
<dependencies>
  <dependency id="VirtoCommerce.ApplicationInsights" version="3.800.0" optional="true" />
</dependencies>
  • Boost Application Insights module using Module Sequence Boost (works only in VirtoCommerce Platform 3.872.0 or later, as require an important bug fix):

appsettings.json

"VirtoCommerce": {
    "ModuleSequenceBoost": [
      "VirtoCommerce.ApplicationInsights"
    ]
}

or environments.yml (VirtoCloud)

VirtoCommerce__ModuleSequenceBoost__0: VirtoCommerce.ApplicationInsights
1 Like

Also, this post is related to the error: Could not load module VirtoCommerce.ApplicationInsights 3.802.0. Error: System.NullReferenceException: Object reference not set to an instance of an object. …

1 Like