Deadlock when using Autofac and the ConsoleLifetime on exit
See original GitHub issueI’m trying to use Autofac
as my service provider in a console application. Here is a minimal program to repro this:
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public class Program
{
public static async Task Main(string[] args) => await new HostBuilder()
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.RunConsoleAsync();
}
It is causing a deadlock because their provider doesn’t implement IDisposable
. Unfortunately, the ConsoleLifetime
locks until it is disposed, and this only happens if:
- The
IServiceProvider
implementsIDisposable
- The
IServiceProvider
disposes allIDisposable
instances it creates
Autofac
opted not to implement IDisposable
in the past because if the service provider is supposed to be disposed, they should have put IDisposable on it
(from https://github.com/autofac/Autofac.Extensions.DependencyInjection/issues/3#issuecomment-242068640). I’ve opened a new issue where I hope they’ll implement it (or allow me to), but I imagine this will be an issue for other DI containers.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (5 by maintainers)
Top Results From Across the Web
Deadlocking on Async even with ConfigurationAwait(false) ...
I'm getting a deadlock on some C# code even though I'm using ConfigureAwait(false). Unfortunately I'm not able to use async all-the-way up ...
Read more >The curious case of a deadlock in Autofac
This tells Autofac that every time my code needs an instance of IQueryProcessor (either through a constructor or property dependency or ...
Read more >Handling Concurrency — Autofac 7.0.0 documentation
Autofac is designed in such a way that deadlocks won't occur in normal use. This section is a guide for maintainers or extension...
Read more >(PDF) ASP NET CORE | Anderson Louzada
Substitua o código do modelo pelo seguinte código: using Microsoft.EntityFrameworkCore; namespace TodoApi. ... NET Core, consulte a documentação do Autofac.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a workaround, I’ve been able to do the following:
I’m not sure the side-effects are worth it. We’ll get back to you.