azure-functions-host: Injecting TelemetryConfiguration no longer works after updating from v3-Preview to v3

Following the instructions in https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#version-2x-3 used to work in the v3-preview but when updating to v3 + netcoreapp3.1 TelemetryConfiguration is no longer available and function blow up with:

Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration'

This commit shows the details of the upgrade attempt:

https://github.com/andreasohlund/PublicApi/commit/2783a2b1d34beaf64d5ca82f107087239879c73a

"APPINSIGHTS_INSTRUMENTATIONKEY": "..." is set in local.setting.json

Could it be the the v3 host no longer enable telemetry when running locally?

Investigative information

Please provide the following:

  • Timestamp:
  • Function App version (1.0 or 2.0): v3
  • Function App name: PublicApiBackend
  • Function name(s) (as appropriate): N/A
  • Invocation ID:N/A
  • Region:N/A

Repro steps

Provide the steps required to reproduce the problem:

  1. In v3-Preview inject a TelemetryClient or TelemetryConfiguration
  2. Update to v3 and see it fail when running locally

Expected behavior

Should inject the TelemetryConfiguration according to https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#version-2x-3

Actual behavior

Injection fails with

Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration'

Known workarounds

None at the stage

Related information

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 39 (6 by maintainers)

Most upvoted comments

@fabiocav You’ve closed this with the merge of the fix, but when can we expect the release train to push out updated packages that actually resolve this for end users?

To avoid break the function code or to have to deal with obscure MSBuild props, we went to the simplest route for now:

builder.Services.AddSingleton<TelemetryConfiguration>(sp =>
            {
                var key = Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY");
                if (!string.IsNullOrWhiteSpace(key))
                {
                    return new TelemetryConfiguration(key);
                }
                return new TelemetryConfiguration();
            });

Although it is a ridiculous solution, it at least works for now and don’t have the side-effects other had mentioned here…

This is a critical problem that should be catch on the release tests as it is one of the built-in features provided by the host… I hope some fix would come soon…

@dougxwok2 – this will be in the next release payload which should start within the next couple of business days. I’d tentatively say it’ll be everywhere within 2 weeks.

It’s part of https://github.com/Azure/azure-functions-host/releases/tag/v2.0.12998. The rollout has started. Unless we hit snags it should be everywhere later next week.

@brettsam , can this patch be expedited? The workaround is causing other issues, and the inability to inject TelemetryConfiguration in v3 is affecting customer projects and deliverables …

Using Microsoft.Azure.WebJobs.Logging.ApplicationInsights also didn’t help me. It’s the first thing I tried.

@JAugustusSmith What you are doing is just re-adding it to the service collection. That’s not how this is supposed to work. Functions are adding its own stuff that we want to resolve. So dont just blindly add the above code without confirmation from microsoft that this is how they want us to do it now. Its a breaking change from 2.0

It was the only way I could get it to work with the current bug. I’m not leaving it in as a solution, but it was either this, or go to 2.2

Using Microsoft.Azure.WebJobs.Logging.ApplicationInsights (v 3.0.14) did not resolve the issue for me.

What version of App Insights are you referencing in your project? I’d recommend referencing the latest https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/, rather than the App Insights package itself. Sometimes they move ahead of where we are (we are typically a couple weeks behind) and these odd type resolution issues hit.

I got it to work by Injecting the TelemetryClient manually. This is against what the documentation says in v2

[assembly: FunctionsStartup(typeof(FunctionApp7.Startup))]

namespace FunctionApp7 { public class Startup : FunctionsStartup { public override void Configure(IFunctionsHostBuilder builder) { builder.Services.AddApplicationInsightsTelemetry(); } } }