title: .net Core Console application in Docker Container throws System.ObjectDisposedException
body: <p>i found many entries to this issue but no solution worked for me.</p>
  
  <p>In short i have a .net Core 3.1 console application which i want to run in Docker. If i start the Application in Visual Studio or via the commandline all works fine (Debug and Release build).</p>
  
  <p>If i put it in a Docker container, i get the following exception:</p>
  
  <pre><code>Unhandled exception. System.ObjectDisposedException: Cannot access a disposed object.
  Object name: 'IServiceProvider'.
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
  at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
  </code></pre>
  
  <p>My Program.cs looks like this:</p>
  
  <pre><code>using System;
  using System.Runtime.Loader;
  using System.Threading;
  using Microsoft.Extensions.DependencyInjection;
  
  namespace testapp
  {
      internal class Program
      {
          private static IServiceProvider _serviceProvider;
  
          private static void Main(string[] args)
          {
              Startup.Init(out _serviceProvider);
  
              var ended = new ManualResetEventSlim();
              var starting = new ManualResetEventSlim();
  
              var dataStorage = _serviceProvider.GetService&lt;IDataStorageService&gt;();
  
              AssemblyLoadContext.Default.Unloading += ctx =&gt;
              {
                  Console.WriteLine("Unloding fired");
                  starting.Set();
                  Console.WriteLine("Waiting for completion");
                  ended.Wait();
              };
  
              Console.WriteLine("Waiting for signals");
              starting.Wait();
  
              Console.WriteLine("Received signal gracefully shutting down");
              Thread.Sleep(5000);
              ended.Set();
          }
  
      }
  }
  </code></pre>
  
  <p>And my Startup.cs</p>
  
  <pre><code>using System;
  using System.Collections.Specialized;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Serilog;
  using Serilog.Sinks.Elasticsearch;
  
  namespace testapp
  {
      public static class Startup
      {
          public static void Init(out IServiceProvider serviceProvider)
          {
              Injection(out serviceProvider);
          }
  
          private static void Injection(out IServiceProvider serviceProvider)
          {
              var builder = new HostBuilder()
                  .ConfigureServices((hostContext, services) =&gt;
                  {
  
                      services.AddSingleton&lt;IDataStorageService, DataStorageService&gt;();
  
  
                      services.AddLogging(configure =&gt; configure.AddSerilog());
                  }).Build();
  
              serviceProvider = builder.Services;
  
              var configService = serviceProvider.GetService&lt;IConfigService&gt;();
  
              Console.WriteLine("ElasticUrl: " + configService.GetElasticEndpoint());
  
              Log.Logger = new LoggerConfiguration()
                  .Enrich.FromLogContext()
                  .WriteTo.File("consoleapp.log")
                  .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(configService.GetElasticEndpoint()))
                  {
                      AutoRegisterTemplate = true,
                      TypeName = "_doc",
                      ModifyConnectionSettings = c =&gt; c.GlobalHeaders(new NameValueCollection
                          {{"Authorization", configService.GetElasticAuthentication()}})
                  })
                  .CreateLogger();
  
              builder.RunAsync();
          }
      }
  }
  </code></pre>
  
  <p>The Exception is thrown at this line in Program.cs:</p>
  
  <pre><code>var dataStorage = _serviceProvider.GetService&lt;IDataStorageService&gt;();
  </code></pre>
  
  <p>In the Startup.cs the line <code>var configService = serviceProvider.GetService&lt;IConfigService&gt;();</code> is working.</p>
  
  <p>As i mentioned, all is running and working in visual Studio or if i launch the commandline tool within the CLI. 
  Only in Docker the Exception occures.
  Here is the DockerFile:</p>
  
  <pre><code>FROM mcr.microsoft.com/dotnet/core/runtime:3.1
  
  COPY bin/Release/netcoreapp3.1/publish/ app/
  
  ENV TZ=Europe/Berlin
  ENV ASPNETCORE_ENVIRONMENT=Production
  
  RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime &amp;&amp; echo $TZ &gt; /etc/timezone
  
  ENTRYPOINT ["dotnet", "app/testapp.dll"]
  </code></pre>
  
  <p>thanks for any help! </p>