question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Deadlock when using Autofac and the ConsoleLifetime on exit

See original GitHub issue

I’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:

  1. The IServiceProvider implements IDisposable
  2. The IServiceProvider disposes all IDisposable 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:closed
  • Created 5 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
abe545commented, Jun 6, 2018

As a workaround, I’ve been able to do the following:

using System;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

public class Program
{
    public static async Task Main(string[] args)
    {
        IDisposable container = null;
        await new HostBuilder()
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureContainer<ContainerBuilder>((_, cb) => cb.RegisterBuildCallback(c => container = c))
            .RunConsoleAsync();
        container?.Dispose();
    }
}
1reaction
Tratchercommented, Jun 6, 2018

I’m not sure the side-effects are worth it. We’ll get back to you.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found