title: .NET Core Dependency Injection container not calling Dispose()
body: <p>I'm at a loss here with why <code>Dispose()</code> isn't being called when the program ends. I wrote a console application to facilitate testing an Azure Functions App. One of the classes implements <code>IDisposable</code> but <code>Dispose()</code> is never being called. I expanded my example to see if a dependent service that implements <code>IDisposable</code> has it's <code>Dispose()</code> method called and it does not. I just updated to 16.9.4 this morning and target framework is .NET 5. Here's the sample code:</p>
  <pre class="lang-cs prettyprint-override"><code>public class CustomOptions
  {
      public const string Section = &quot;CustomSettings&quot;;
      public string Url { get; set; }
      public bool UseHttps { get; set; }
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>public interface IDisposableService
  {
      void DoSomething();
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>public class DisposableService : IDisposableService, IDisposable
  {
      public void Dispose()
      {
          Console.WriteLine(&quot;Disposing a disposable service&quot;);
          GC.SuppressFinalize(this);
      }
  
      public void DoSomething()
      {
          Console.WriteLine(&quot;I'm doing some work&quot;);
      }
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>public interface IConsoleService
  {
      void Run(string[] args);
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>public class ConsoleService : IConsoleService, IDisposable
  {
      private readonly ILogger&lt;ConsoleService&gt; _logger;
      private readonly CustomOptions _options;
      private readonly IDisposableService _service;
  
  
      public ConsoleService(IDisposableService service,
                            ILogger&lt;ConsoleService&gt; logger,
                            IOptions&lt;CustomOptions&gt; options)
      {
          _service = service;
          _logger = logger;
          _options = options.Value;
      }
  
      public void Dispose()
      {
          Console.WriteLine($&quot;Disposing {nameof(ConsoleService)}&quot;);
          GC.SuppressFinalize(this);
      }
  
      public void Run(string[] args)
      {
          _logger.LogInformation(&quot;The application is now starting {Start}&quot;, DateTime.Now);
          Console.WriteLine($&quot;The Url in the settings file is '{_options.Url}'&quot;);
          _service.DoSomething();
      }
  }
  </code></pre>
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          IHost host = Host.CreateDefaultBuilder()
              .ConfigureServices((context, services) =&gt;
              {
                  services.Configure&lt;CustomOptions&gt;(
                      context.Configuration.GetSection(CustomOptions.Section));
  
                  services.AddTransient&lt;IDisposableService, DisposableService&gt;();
                  services.AddTransient&lt;IConsoleService, ConsoleService&gt;();
  
                  services.AddLogging();
                  services.AddOptions();
              })
              .Build();
  
          IConsoleService service =
              ActivatorUtilities.CreateInstance&lt;ConsoleService&gt;(host.Services);
          service.Run(args);
      }
  }
  </code></pre>
  <pre class="lang-cs prettyprint-override"><code>appsettings.json
  {
      &quot;Logging&quot;: {
          &quot;LogLevel&quot;: {
              &quot;Default&quot;: &quot;Information&quot;,
              &quot;Microsoft&quot;: &quot;Warning&quot;,
              &quot;Microsoft.Hosting.Lifetime&quot;: &quot;Warning&quot;
          }
      },
      &quot;CustomSettings&quot;: {
          &quot;Url&quot;: &quot;www.contoso.com&quot;,
          &quot;UseHttps&quot;:  true
      }
  }
  </code></pre>
  <p>The output from the program is:<br/></p>
  <blockquote>
  <p>info: ConsoleApp.ConsoleService[0]<br/>
  The application is now starting 04/14/2021 10:30:54<br/>
  The Url in the settings file is 'www.contoso.com'<br/>
  I'm doing some work<br/></p>
  </blockquote>
  <p>Any help is greatly appreciated.</p>
  <hr />
  <p>Changes to get it working are as follows:</p>
  <ol>
  <li>Add <code>IDisposable</code> to <code>IConsoleService</code></li>
  </ol>
  <pre class="lang-cs prettyprint-override"><code>public interface IConsoleService, IDisposable
  {
      void Run(string[] args);
  }
  </code></pre>
  <ol start="2">
  <li>and remove it from <code>ConsoleService</code></li>
  </ol>
  <pre class="lang-cs prettyprint-override"><code>public class ConsoleService : IConsoleService
  {
  </code></pre>
  <ol start="3">
  <li><p>Wrap the <code>IHost</code> variable in a <code>using</code> statement or call <code>host.Dispose()</code></p>
  </li>
  <li><p>Use <code>IServiceProvider.GetService&lt;&gt;()</code> instead of <code>ActivatorUtilities.CreateInstance&lt;&gt;()</code></p>
  </li>
  </ol>
  <pre class="lang-cs prettyprint-override"><code>    host.Services.GetService&lt;IConsoleService&gt;().Run(args);
      host.Dispose();
  </code></pre>