Azure-docs: TelemetryConfiguration does not resolve from DI

Created on 16 Jul 2019  Â·  13Comments  Â·  Source: MicrosoftDocs/azure-docs

Retrieving an instance of TelemetryConfiguration via constructor injection as stated in this document under Log custom telemetry in C# functions throws an error.

An unhandled host error has occurred.
Microsoft.Extensions.DependencyInjection.Abstractions: Unable to resolve service for type 'Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration' while attempting to activate 'LinkyLinkEntities.Endpoints.LinkOperations'.

Other services that I have registered resolve correctly via the DI support in Functions

Using
Azure Functions Core Tools (2.7.1373 Commit hash: cd9bfca26f9c7fe06ce245f5bf69bc6486a685dd)
Function Runtime Version: 2.0.12507.0
Visual Studio 2019 16.2 preview 3

@fabiocav @brettsam

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Pri1 azure-functionsvc cxp doc-bug triaged

All 13 comments

@cecilphillip Thank you for your feedback . We will have this reviewed with engineering and update the article as needed.

@cecilphillip Make sure you have the latest nuget package of Microsoft.NET.Sdk.Functions.

@DixitArora-MSFT @shashishailaj Even, I am getting the same issue for the function app while trying to implement custom telemetry logging. I am using the v1.0.29 of the Microsoft.NET.Sdk.Functions nuget package

@cecilphillip - As per the Microsoft documentation - link, Azure Function's inbuilt App Insights support works with the v2.9.1 of App Insights. Once i downgraded the App Insight version to 2.9.1, it started working. This is not working with v2.10.0. I am not sure whether this is a bug or it is an expected behaviour.

I created a new Function app and changed the NuGet package version fro AppInsights, but still getting the same errors.

This still doesn’t work for me. Here are all my details

Code

public  class Function1
{
    private readonly TelemetryConfiguration tconf;

    public Function1(TelemetryConfiguration tconf)
    {
        this.tconf = tconf;
    }
    [FunctionName("Function1")]
    public  async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        return new OkResult();
    }
}

Project file

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
    <AzureFunctionsVersion>v2</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights" Version="2.9.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
  </ItemGroup>

Functions runtime

Azure Functions Core Tools (2.7.1373 Commit hash: cd9bfca26f9c7fe06ce245f5bf69bc6486a685dd)
Function Runtime Version: 2.0.12507.0

@lolekjohn do you have the same version of core tools and the runtime?

@cecilphillip Why are you using

TelemetryConfiguration

in the constructor?

The code snipped was just to validate issue I was having. I’m pulling in TelemetryConfiguration in my current project because I’m using it to initialize TelemetryClient.

Also, I’ve sorted this out with 2.9.1. I needed to add APPINSIGHTS_INSTRUMENTATIONKEY

@cecilphillip and I were chatting on IM -- there's 2 things I think we should do in the docs:

  • Mention that if you're running this locally, you should set APPINSIGHTS_INSTRUMENTATIONKEY in your local app settings. None of the App Insights services are registered unless that key is present, so you'll get errors like this -- or you may not see your ITelemetryInitializers get called.
  • It may be a better approach to reference https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ rather than any App Insights packages directly. This is the package we deploy to production (and the CLI), so its dependencies will match what we support at the current point in time. We always trail behind App Insights release by a few weeks and during this time we do not support the latest bits (we only do binding redirects up to our referenced version, not past). Although, right now we're in a weird state where we do support 2.10.0 in production, but the CLI/nugets haven't been pushed yet (those are the last steps in a release).

@cecilphillip and I were chatting on IM -- there's 2 things I think we should do in the docs:

  • Mention that if you're running this locally, you should set APPINSIGHTS_INSTRUMENTATIONKEY in your local app settings. None of the App Insights services are registered unless that key is present, so you'll get errors like this -- or you may not see your ITelemetryInitializers get called.
  • It may be a better approach to reference https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Logging.ApplicationInsights/ rather than any App Insights packages directly. This is the package we deploy to production (and the CLI), so its dependencies will match what we support at the current point in time. We always trail behind App Insights release by a few weeks and during this time we do not support the latest bits (we only do binding redirects up to our referenced version, not past). Although, right now we're in a weird state where we do support 2.10.0 in production, but the CLI/nugets haven't been pushed yet (those are the last steps in a release).

It would be better if the Documentation in https://docs.microsoft.com/en-us/azure/azure-functions/functions-monitoring#log-custom-telemetry-in-c-functions is updated accordingly so that the correct app insights can be used

@lolekjohn -- my comment says that we need to update the docs... is there something different you'd like the docs to say than what I listed? What do you mean by the "correct app insights"?

@lolekjohn -- my comment says that we need to update the docs... is there something different you'd like the docs to say than what I listed? What do you mean by the "correct app insights"?

My bad. I don't have anything else to be added. I meant correct App Insights nuget package can be used.

Oh, sure -- note that the package I mentioned above does reference the real App Insights as a dependency, it just may be slightly behind the newest public version.

Some more details for anyone else that may stumble across this:

The scenario where you get into these cases where types aren't found, using the current state as an example:

  1. Functions references the Microsoft.Azure.WebJobs.Logging.ApplicationInsights package, which depends on AppInsights version 2.9.1.
  2. App Insights releases version 2.10.0 to nuget.
  3. Functions absorbs that change but it takes several weeks to make it to production. During this time, any Function app referencing this newer package will not find these types (the .NET type system doesn't see TelemetryClient v2.9.1 and TelemetryClient v2.10.0 as the same type, and binding redirects only redirect up to the current version, not past).
  4. Functions changes eventually get out to production.
  5. Functions tools and nugets that correspond to production eventually get pushed publicly, and now everyone can use 2.10.0 everywhere.

We're currently waiting for step 5 to complete, so local references to 2.10.0 won't find these types in DI.

I've also logged https://github.com/Azure/azure-functions-host/issues/4682 to see if we can bake some of this directly into the host -- if we show you a warning or error if you get ahead of the host references,.

@cecilphillip Brettsam has already open another issue for tracking. So for now I will proceed with the closure of this.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JeffLoo-ong picture JeffLoo-ong  Â·  3Comments

AronT-TLV picture AronT-TLV  Â·  3Comments

spottedmahn picture spottedmahn  Â·  3Comments

Agazoth picture Agazoth  Â·  3Comments

paulmarshall picture paulmarshall  Â·  3Comments