azure-functions-host: Dependency Injection issue: Unable to resolve service for type ... while attempting to activate [TimerFunction]

Injecting a DocumentClient instance works fine when debugging a Function locally but not on Azure. Possibly the same symptom as #4187.

Investigative information

Please provide the following:

  • Timestamp: 2019-09-17 11:30:00.006
  • Function App version (1.0 or 2.0): ~2
  • Function App name:
  • Function name(s) (as appropriate):
  • Invocation ID: 1a2d55843b4483409d843976bb3b7fdf
  • Region: UK South

Repro steps

Configure DocumentClient as a service:

public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddSingleton<IDocumentClient>(s =>
                new DocumentClient(new Uri(GetEnvironmentVariable("DatabaseAcctEndPoint")), GetEnvironmentVariable("DatabaseAcctKey") ));
        }
    }

… and then inject:

public class TimerClass
{
    [snip]
    private IDocumentClient _documentClient;
    
    public TimerClass(IDocumentClient documentClient)
    {
        _documentClient = documentClient;
    }

Expected behavior

The above code works fine when running in Debug locally (whether connecting to CosmosDB emulator or Azure-hosted CosmosDB) but not when the function is hosted on Azure.

Actual behavior

Exception thrown:

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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 37)
   at Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 32)
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1+<>c__DisplayClass1_1.<.ctor>b__0 (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 20)
   at Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory`1.Create (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 26)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 44)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+ParameterHelper.Initialize (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 846)
   at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor+<TryExecuteAsyncCore>d__16.MoveNext (Microsoft.Azure.WebJobs.Host, Version=3.0.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35Microsoft.Azure.WebJobs.Host, Version=3.0.13.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.13.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35: 117)

Related information

I think this is the same as, or similar to, #4187. As @fabiocav suggested at the end of that issue, I’m opening this as a new issue.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 43 (8 by maintainers)

Most upvoted comments

I have tried all of these solutions and it is still not working for me. This should NOT be closed.

This hasn’t gone stale and certainly shouldn’t be closed. I’ve been waiting quietly for a resolution. I dont see any resolutions posted on this thread and recent testing is still failing. In addition, my problem, as detailed above, is failing locally, but works when deployed to Azure.

I had the same problem. I fixed it by REMOVING the Microsoft.Azure.Functions.Extensions NuGet package. As soon as I did this IoC worked and I can debug.

We were facing same issue for version 2.0.12961.0 and issue got resolved after updating default host.json

Before- { "version": "2.0", "extensionBundle": { "id": "Microsoft.Azure.Functions.ExtensionBundle", "version": "[1.*, 2.0.0)" } }

Working- { "version": "2.0" }

I am having this issue after updating to AzureFunctionsVersion v3. It worked fine with v2. Is there another issue tracking v3 dependency injection issues or is that covered by this issue?

@Invre Actually it is combination of both. Upgrade to 3.0.5 and remove the ExtensionBundles. Tested with 3.0.4 without bundles it didn’t worked.

Di working fine in 3.0.3 for us… updating to 3.0.4 breaks it.

I also wanted to confirm that DI is busted with 3.0.4 sdk. Works fine with 3.0.3. I just had to downgrade my new solution in order get DI working.

@ThatBlokeCalledJay are you seeing the problem with the same setup as described in the original issue? Would you mind opening a separate issue with the details of what you’re seeing and a repro?

@fabiocav of course I am. You’d like me to copy the verbose information + pictures + link to an example repo I posted above to a new thread? 90% of all information on this thread has been provided by me, heaps of info, images and the requested repo. I think a better idea would be for you to re-open this thread as it should never of been closed in the first place. I am having exactly the same issue now as I was having when I detailed my first issues.

I’ve already put in the leg work trying to detail as much as I can to help resolve the issue. Sorry if I didn’t keep poking the thread every 15 minutes to keep it alive.

I found that if IWebJobsStartup.Configure method is async and there is awaitable calls in this method, then “Unable to resolve service for type…” exception is thrown.

Not works:

public async void Configure(IWebJobsBuilder builder) // async method
{
    var backupConfiguration = await configurationReader.ReadConfiguration<BackupConfiguration>(configurationName);
    new Startup().Configure(builder.Services, backupConfiguration);
}

Works:

public void Configure(IWebJobsBuilder builder) // non-async method
{
    var backupConfiguration = configurationReader.ReadConfiguration<BackupConfiguration>(configurationName).Result;
    new Startup().Configure(builder.Services, backupConfiguration);
}

@adiazcan This is default host.json You have to change properties of host.json file to copy always.

Same issue and behavior than @kabirshaikh . Any help would be appreciated.

I am having this issue as well. More specifically, it works locally, but fails on Azure. I have tried the various proposed solutions mentioned in this thread, but nothing has worked. Is this issue being worked on?