commercetools/commercetools-dotnet-core-sdk

Override the Correlation ID provider

Closed this issue · 10 comments

How does one override the correlation ID (instead of using the default one) provider?

You can create your own custom ICorrelationIdProvider and inject it in the services container, it will be something like this:
DefaultCorrelationIdProvider

Please let me know if you have any question

Yeah that part I figured out. I just can't figure out how to put it inside the container. The current place this is registered is deep inside private methods.

Just as a hint: services.UseCommercetools().AddSingleton(ICorrelationIdProvider, YourCorreletionIdProvider)

This should override the internally registered default provider

So I tried:


            var ct = services.UseCommercetools(new ConfigurationWrapper(settings));
            ct.Services.AddSingleton<ICorrelationIdProvider, AppInsightsCorrelationIDHandler>();

and


            services.UseCommercetools(new ConfigurationWrapper(settings));
            services.AddSingleton<ICorrelationIdProvider, AppInsightsCorrelationIDHandler>();

As the result of UseCommercetools is an IHttpClientBuilder and doesn't seem to have AddSingleton.

Neither works. What am I missing?

Ah sorry then it‘s

services.UseCommercetools;
services.AddSingleton

That's what I tried and doesn't seem to ever create AppInsightsCorrelationIDHandler or call it.

yes you have to configure your custom services before commercetools services, so move this line

services.AddSingleton<ICorrelationIdProvider, AppInsightsCorrelationIDHandler>(); before UseCommercetools line, so your injected custom service will be retrieved first when it's needed.

Please try it and let me know if this works

not here. But maybe I'm doing something wrong:

    public class AppInsightsCorrelationIDHandler : ICorrelationIdProvider
    {
        public AppInsightsCorrelationIDHandler()
        {

        }
        public string CorrelationId => Activity.Current?.Id ?? Guid.NewGuid().ToString();
    }
...

            services.AddSingleton<ICorrelationIdProvider, AppInsightsCorrelationIDHandler>();
            services.UseCommercetools(new ConfigurationWrapper(settings));

I just merged the PR which has the fix for this issue, I believe you can now inject your custom correlationProvider service before UseCommercetools

working perfectly! Thanks @MicheleRezk