title: Azure Functions Scoped instances are disposed and no longer available
body: <p>I am receiving below error when function app is running on the run-time version "<code>2.0.12701.0" (FUNCTIONS_EXTENSION_VERSION = ~2 in App Settings)</code>. As soon I change the <code>FUNCTIONS_EXTENSION_VERSION to 2.0.12673.0</code> in App Settings. function starts working without any code change or deployment.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>DryIoc.ContainerException : Scope disposed{no name, Parent=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) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 8991    at DryIoc.Scope.TryGet(Object&amp; item,Int32 id) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7881    at
    DryIoc.Container.InstanceFactory.GetAndUnwrapOrDefault(IScope
    scope,Int32 factoryId) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 1480    at
    DryIoc.Container.InstanceFactory.GetInstanceFromScopeChainOrSingletons(IResolverContext
    r) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 1466    at
    DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type
    serviceType,IfUnresolved ifUnresolved) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 224    at DryIoc.Container.DryIoc.IResolver.Resolve(Type
    serviceType,IfUnresolved ifUnresolved) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 195    at DryIoc.Resolver.Resolve[TService](IResolver
    resolver,IfUnresolved ifUnresolved) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 4742    at
    DryIoc.Microsoft.DependencyInjection.DryIocAdapter.&lt;>c__DisplayClass3_0.b__0(IResolverContext
    r) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\DryIocAdapter.cs
    : 156    at
    DryIoc.Registrator.&lt;>c__DisplayClass27_0.b__0(IResolverContext
    r) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 4541    at lambda_method(Closure ,IResolverContext )    at
    DryIoc.Factory.&lt;>c__DisplayClass26_1.b__2() at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 6596    at DryIoc.Scope.TryGetOrAdd(ImMap<code>1 items,Int32
    id,CreateScopedValue createValue,Int32 disposalOrder) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7840    at DryIoc.Scope.GetOrAdd(Int32 id,CreateScopedValue
    createValue,Int32 disposalOrder) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7825    at DryIoc.Factory.ApplyReuse(Expression serviceExpr,Request
    request) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 6595    at DryIoc.Factory.GetExpressionOrDefault(Request request) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 6555    at DryIoc.Factory.GetDelegateOrDefault(Request request) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 6625    at DryIoc.DelegateFactory.GetDelegateOrDefault(Request
    request) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7731    at DryIoc.Container.DryIoc.IResolver.Resolve(Type
    serviceType,Object serviceKey,IfUnresolved ifUnresolved,Type
    requiredServiceType,Request preResolveParent,Object[] args) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 290    at lambda_method(Closure )    at
    DryIoc.Scope.TryGetOrAdd(ImMap</code>1 items,Int32 id,CreateScopedValue
    createValue,Int32 disposalOrder) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7840    at DryIoc.Scope.GetOrAdd(Int32 id,CreateScopedValue
    createValue,Int32 disposalOrder) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 7825    at lambda_method(Closure ,IResolverContext )    at
    DryIoc.Container.ResolveAndCacheDefaultFactoryDelegate(Type
    serviceType,IfUnresolved ifUnresolved) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 224    at DryIoc.Container.DryIoc.IResolver.Resolve(Type
    serviceType,IfUnresolved ifUnresolved) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\DryIoc\Container.cs
    : 195    at
    Microsoft.Azure.WebJobs.Script.WebHost.DependencyInjection.ScopedServiceProvider.GetService(Type
    serviceType) at
    C:\azure-webjobs-sdk-script\src\WebJobs.Script.WebHost\DependencyInjection\ScopedServiceProvider.cs
    : 25    at
    Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider
    sp,Type type,Type requiredBy,Boolean isDefaultParameterRequired)    at
    lambda_method(Closure ,IServiceProvider ,Object[] )    at
    Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IServiceProvider
    serviceProvider) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs
    : 37    at
    Microsoft.Azure.WebJobs.Host.Executors.DefaultJobActivator.CreateInstance[T](IFunctionInstanceEx
    functionInstance) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\DefaultJobActivator.cs
    : 32    at
    Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory<code>1.&lt;&gt;c__DisplayClass1_1.&lt;.ctor&gt;b__0(IFunctionInstanceEx
    i) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs
    : 20    at
    Microsoft.Azure.WebJobs.Host.Executors.ActivatorInstanceFactory</code>1.Create(IFunctionInstanceEx
    functionInstance) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\ActivatorInstanceFactory.cs
    : 26    at
    Microsoft.Azure.WebJobs.Host.Executors.FunctionInvoker`2.CreateInstance(IFunctionInstanceEx
    functionInstance) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionInvoker.cs
    : 44    at
    Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.ParameterHelper.Initialize()
    at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs
    : 846    at async
    Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.TryExecuteAsyncCore(IFunctionInstanceEx
    functionInstance,CancellationToken cancellationToken) at
    C:\projects\azure-webjobs-sdk-rqm4t\src\Microsoft.Azure.WebJobs.Host\Executors\FunctionExecutor.cs
    : 116</p>
  </blockquote>
  
  <p>Changing the Function run-time version (FUNCTIONS_EXTENSION_VERSION =2.0.12673.0) in App Setting from default run-time version 2.0.12701.0 fixed the issue.</p>
  
  <p>This is my Function App Startup class which register dependencies.</p>
  
  <pre class="lang-cs prettyprint-override"><code> public class FunctionAppStartup : FunctionsStartup
      {
          public override void Configure(IFunctionsHostBuilder builder)
          {
              var config = new ConfigurationBuilder()
                                  .SetBasePath(Environment.CurrentDirectory)
                                  .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                  .AddJsonFile("secret.settings.json", optional: true, reloadOnChange: true)
                                  .AddEnvironmentVariables()
                                  .Build();
  
              builder.Services.Configure&lt;ConfigurationManager&gt;(x =&gt; config.Bind(x));
              builder.Services.AddSingleton&lt;Contracts.IConfiguration&gt;(resolver =&gt;
                  resolver.GetRequiredService&lt;IOptions&lt;ConfigurationManager&gt;&gt;().Value);
  
              builder.Services.AddScoped&lt;IChannelAdapter, ChannelAdapter&gt;();
              builder.Services.AddScoped&lt;ITableStorageManager, TableStorageManager&gt;();
              builder.Services.AddSingleton&lt;IDatabaseContext, DatabaseContext&gt;();
              builder.Services.AddScoped&lt;IJobRepository, JobRepository&gt;();
          }
      }
  </code></pre>
  
  <p>Expecting my function app to run on the latest run-time version instead of me targeting a minor run-time version.</p>