azure-functions-host: HttpClientFactory + DryIoc.ContainerException

Issue Description

We are building RESTFul backend using Azure Functions. We are leveraging HttpClientFactory via DI which seems to work for about an hour and then starts to throw following exception:

exception:DryIoc.ContainerException: Scope disposed{no name, Parent=disposed{no name}} is disposed and scoped instances are disposed and no longer available.

Side note: our issue resembles the bug reported here #5590.

Investigative information

  • Timestamp: March 31, 2020
  • Function App version: Runtime version is 3.0.13139.0 (~3)
  • Function App name: not providing
  • Function name(s) (as appropriate): not providing
  • Invocation ID: 6a5cacf8-c872-41c4-a940-cadf712e5b5a and 04/01/2020 19:32:28
  • Region: west us 2

Project Setup:

Nuget packages: note removed few packages that were not relevant

<PropertyGroup>
  <TargetFramework>netcoreapp3.1</TargetFramework>
  <AzureFunctionsVersion>v3</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Microsoft.Azure.Cosmos" Version="3.7.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.2" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="3.1.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<ItemGroup>

Startup (DI registration):

 public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.AddHttpClient();
            builder.Services.AddTransient<IAPIService, APIService>();

            ... other services here ....
        }
    }

API Service:

public class APIService : IAPIService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public ApiService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public void MakeRequest()
        {
            var httpClient = _httpClientFactory.CreateClient();
            httpClient.PostAsync(...);
        }
    }

Function Call:

public class MyFunction
    {
        private readonly IAPIService _apiService;

        public MyTrigger(IAPIService apiService)
        {
            _apiService = apiService;
        }
// Runs every hour
[FunctionName(nameof(MyTimerTrigger))]
        public async Task MyTimerTrigger([TimerTrigger("0 23 * * * *", RunOnStartup = false)]TimerInfo timerInfo, ILogger logger)
        {
            logger.LogStartFunction();

            await _apiService.Process();

            logger.LogExitFunction();
        }

Expected behavior

This timer based function and other RESTFul functions continue to serve requests.

Actual behavior

After one hour or so of being operational, all services start returning 500 and following exception is logged in the App Insight:

Exception: function:MyFunctions.MyEndPoint msg: exception:DryIoc.ContainerException: Scope disposed{no name, Parent=disposed{no name}} is disposed and scoped instances are disposed and no longer available. at DryIoc.Throw.It(Int32 error, Object arg0, Object arg1, Object arg2, Object arg3) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 8990 at DryIoc.Scope.TryGet(Object& item, Int32 id) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 7880 at DryIoc.Container.InstanceFactory.GetAndUnwrapOrDefault(IScope scope, Int32 factoryId) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 1479 at DryIoc.Container.InstanceFactory.GetInstanceFromScopeChainOrSingletons(IResolverContext r) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 1468 at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, Object serviceKey, IfUnresolved ifUnresolved, Type requiredServiceType, Request preResolveParent, Object[] args) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 307 — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source) at System.Linq.Expressions.Interpreter.ExceptionHelpers.UnwrapAndRethrow(TargetInvocationException exception) at System.Linq.Expressions.Interpreter.MethodInfoCallInstruction.Run(InterpretedFrame frame) at System.Linq.Expressions.Interpreter.Interpreter.Run(InterpretedFrame frame) at System.Linq.Expressions.Interpreter.LightLambda.Run(Object[] arguments) at Thunk(Func2 , IResolverContext ) at DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type serviceType, IfUnresolved ifUnresolved) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 223 at DryIoc.Container.DryIoc.IResolver.Resolve(Type serviceType, IfUnresolved ifUnresolved) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs:line 194 at Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.ScopedServiceProvider.GetService(Type serviceType) in D:\a\1\s\src\WebJobs.Script.WebHost\DependencyInjection\ScopedServiceProvider.cs:line 25 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandlerEntry(String name) at Microsoft.Extensions.Http.DefaultHttpClientFactory.<>c__DisplayClass14_0.<.ctor>b__1() at System.Lazy1.ViaFactory(LazyThreadSafetyMode mode) — End of stack trace from previous location where exception was thrown — at System.Lazy1.CreateValue() at System.Lazy1.get_Value() at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateHandler(String name) at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name) at System.Net.Http.HttpClientFactoryExtensions.CreateClient(IHttpClientFactory factory)

Possible workarounds (do not know if following is MS recommended approach)

Since DI is not supported for HttpClient, we will manually create an instance and use that.

public class APIService : IAPIService
    {
        private readonly HttpClient _client;

        public APIService()
        {
            _client = new HttpClient();
        }

        [FunctionName("GetPosts")]
        public async Task<IActionResult> Get(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "posts")] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            var res = await _client.GetAsync("https://microsoft.com");
            await _service.AddResponse(res);

            return new OkResult();
        }
    }

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 8
  • Comments: 19 (1 by maintainers)

Most upvoted comments

@ankitkumarr @fabiocav We are seeing this issue as well. We are an internal MS team so if you would like us to share more details directly with you, just let me know.

No workaround. Problem is with scoped DI entities See https://github.com/Azure/azure-functions-host/issues/4914

This issue is still, I did the same change as @felipementel did regarding as a temporary solution – replaced HttpClientFactory.CreateClient() with new HttpClient().

But this is not the best solution, cause creating http client constantly may run out of http port pool. Using “HttpClientFactory” is still the correct approach. Waiting Microsoft team resolve this issue ASAP.

Is this solved? I got the same issue

Our team is experiencing the same issue. Tried to bump referenced Microsoft nuget packages to latest versions, but still fails. Last tested using Microsoft.NET.Sdk.Functions 3.0.7

Same here 😦

I’m also experiencing this error in azure functions.

our team is geeting the same error. Can’t inject IHttpClientFactory in AZF. We’re using Microsoft.Extensions.Http Version=“3.1.5”, Microsoft.NET.Sdk.Functions Version=“1.0.31”, Microsoft.Extensions.Http Version=“3.1.5”

Seeing this behavior as well. 😦