Quantcast
Channel: Brian Pedersen's Sitecore and .NET Blog
Viewing all articles
Browse latest Browse all 286

Error CS0121: The call is ambiguous between the following methods or properties: AddApplicationInsightsTelemetryProcessor

$
0
0

So I wanted to add a ITelemetryProcessor to my project. I created a DependencyTelemetryFilter and applied the filter as I always do:

services
  .AddApplicationInsightsTelemetryProcessor<DependencyTelemetryFilter>();

But this throws an Error CS0121: The call is ambiguous between the following methods or properties error:

The call is ambiguous between the following methods or properties: ‘Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryProcessor<T>(Microsoft.Extensions.DependencyInjection.IServiceCollection)’ and ‘Microsoft.Extensions.DependencyInjection.ApplicationInsightsExtensions.AddApplicationInsightsTelemetryProcessor<T>(Microsoft.Extensions.DependencyInjection.IServiceCollection)

MOST LIKELY ERROR:

You probably have added both of these NuGet packages:

AspNetCore is meant for web projects, and WorkerService is meant for worker services. It is very unlikely that you need to reference both in the same project.

THE HACK IF YOU NEED BOTH NUGET PACKAGES:

In my project I am referencing both Microsoft.ApplicationInsights.AspNetCore and Microsoft.ApplicationInsights.WorkerService, as the project contains both a web project (hence the AspNetCore reference) and a worker service (hence the WorkerService reference).

Now, this is by no means recommended. However, this is my reality, and I need to work around it. To do so, I’ll create a Factory class and inject the ITelemetryProcessor manually. There is 2 classes in this piece of code:

using Microsoft.ApplicationInsights.Channel;
using Microsoft.ApplicationInsights.DataContracts;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.WorkerService;

namespace MyCode
{
  public class DependencyTelemetryFilterFactory : ITelemetryProcessorFactory
  {
    public ITelemetryProcessor Create(ITelemetryProcessor nextProcessor)
    {
      return new DependencyTelemetryFilter(nextProcessor);
    }
  }

  public class DependencyTelemetryFilter : ITelemetryProcessor
  {
    private readonly ITelemetryProcessor _nextProcessor;

    public DependencyTelemetryFilter(ITelemetryProcessor nextProcessor)
    {
      _nextProcessor = nextProcessor;
    }

    public void Process(ITelemetry telemetry)
    {
      if (telemetry is DependencyTelemetry dependencyTelemetry)
      {
        if (dependencyTelemetry.Success == true)
        {
          return;
        }
      }

      _nextProcessor.Process(telemetry);
    }
  }
}

Then I manually inject the processor:

services.AddSingleton<ITelemetryProcessorFactory>(builder => new DependencyTelemetryFilterFactory());

That’s it. You are now a .net core mixed martial arts expert. Happy coding.

MORE TO READ:


Viewing all articles
Browse latest Browse all 286

Trending Articles