title: Dependency Injection unable to resolve service for type in .NET Core Function App running in Azure
body: <h2>Summary</h2>
  
  <p>I've been following the Microsoft guidance <a href="https://docs.microsoft.com/en-us/azure/azure-functions/functions-dotnet-dependency-injection" rel="nofollow noreferrer">Use dependency injection in .NET Azure Functions</a> to introduce some Dependency Injection (DI) into an Azure Function that our team is creating. When I run locally in Visual Studio 2017 and invoke the function using Fiddler or Postman everything works - I can debug hitting breakpoints and the service being provided via the DI setup are working correctly. When we deploy the same function into Azure, however, we get the following exception:</p>
  
  <blockquote>
    <p><strong>Unable to resolve service for type 'MyLibrary.ITelemetryClient' while
    attempting to activate 'MyFunctionApp.GetRemoteSystemsByFeature'.</strong></p>
  </blockquote>
  
  <h2>Details</h2>
  
  <p>I have a library assembly (.NET Standard 2.0) that exposes an interface, <code>ITelemetryClient</code>, and a default implementation, <code>ApplicationInsightsTelemetryClient</code>. They're pretty straightforward bits of code - I don't think the implementation of <em>these</em> are important to the issue, so I'll skip them for now.</p>
  
  <p>In the solution that defines our function app (.NET Core 2.2) I've made sure the right NuGet packages are installed:</p>
  
  <ul>
  <li>Microsoft.Azure.Functions.Extensions v1.0.0</li>
  <li>Microsoft.Azure.WebJobs.Script.ExtensionsMetadataGenerator v1.1.1</li>
  <li>Microsoft.NET.Sdk.Functions v1.0.28</li>
  </ul>
  
  <p>I have created <code>Startup</code> which inherits from <code>FunctionsStartup</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using System;
  using System.Runtime.CompilerServices;
  using MyLibrary;
  using Microsoft.Azure.Functions.Extensions.DependencyInjection;
  using Microsoft.Extensions.DependencyInjection;
  
  [assembly: FunctionsStartup(typeof(MyFunctionApp.Startup))]
  [assembly: InternalsVisibleTo("MyFunctionApp.Tests")]
  
  namespace MyFunctionApp
  {
      public class Startup : FunctionsStartup
      {
          public override void Configure(IFunctionsHostBuilder builder)
          {
              builder.Services.AddSingleton&lt;MyLibrary.ITelemetryClient, MyLibrary.ApplicationInsightsTelemetryClient&gt;();
          }
      }
  }
  </code></pre>
  
  <p>I then give my function a constructor that asks for the <code>MyLibrary.ITelemetryClient</code>:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.AspNetCore.Http;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.Azure.WebJobs;
  using Microsoft.Azure.WebJobs.Extensions.Http;
  using Microsoft.Extensions.Logging;
  using System;
  using System.Threading.Tasks;
  using MyLibrary;
  using MyRepository;
  using MyService;
  
  namespace MyFunctionApp
  {
      public class GetRemoteSystemsByFeature
      {
          private readonly MyLibrary.ITelemetryClient _telemetryClient;
  
          public GetRemoteSystemsByFeature(MyLibrary.ITelemetryClient telemetryClient)
          {
              _telemetryClient = telemetryClient;
          }
  
          // snip - the rest shouldn't matter for this question
      }
  }
  </code></pre>
  
  <p>So I <em>think</em> I've done everything right. And as I said when I debug locally the whole thing works and the correct <code>ITelemetryClient</code> implementation is used and successfully does its thing.</p>
  
  <p>When it gets deployed into Azure, though, we get the error quoted above. The stack trace for that exception doesn't mention any of our code:</p>
  
  <pre><code>System.InvalidOperationException:
     at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService (Microsoft.Extensions.DependencyInjection.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
     at lambda_method (Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)
     at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 42)
     at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 32)
     at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1+&lt;&gt;c__DisplayClass1_1.&lt;.ctor&gt;b__0 (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 20)
     at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.Create (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 26)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 44)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+ParameterHelper.Initialize (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 845)
     at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+&lt;TryExecuteAsyncCore&gt;d__16.MoveNext (Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.csMicrosoft.Azure.WebJobs.Host, Version=3.0.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 116)
  </code></pre>
  
  <p>Any ideas?</p>
  
  <h2>Update</h2>
  
  <p>It was requested I share the contents of the <code>extensions.json</code> file:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
    "extensions":[
      { "name": "Startup", "typeName":"MyFunctionApp.Startup, MyFunctionApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
    ]
  }
  </code></pre>