Second Mediator Call Fails with ObjectDisposedException in ASP.NET Core Using Autofac: “Instances cannot be resolved as it has already been disposed”

I am trying to run two mediator calls inside a method. For some reason, the second mediator call always fail. I am expecting both calls to run without error. The full error message is:

System.ObjectDisposedException: Instances cannot be resolved and
nested lifetimes cannot be created from this LifetimeScope as it (or
one of its parent scopes) has already been disposed

A simplified version of my code is:

// MainLogic.cs (registered as Transient)

private readonly IDateTimeProvider _dateTimeProvider; // Registered as Singleton
private readonly IMediator _mediator; // Registered with Autofac, which defaults to transient

public MainLogic(IDateTimeProvider dateTimeProvider, IMediator mediator)
{
    _dateTimeProvider = dateTimeProvider;
    _mediator = mediator;
}

public async Task<List<myObj>> MainProcessingLogic(string key, List<SomeObj> listOfItems, DateTime dateTime)
{
    try
    {
        var myVar = (await _mediator.Send(new ProcessSomeQuery(key, listOfItems, dateTime))).FirstOrDefault();
    }
    catch (Exception ex)
    {
        _logger.LogError("ProcessSomeQuery ran into an error : " + ex);
    }

    try
    {
        var myVar = await _mediator.Send(new ProcessSomeOtherDifferentQuery(key, listOfItems, dateTime));
    }
    catch (Exception ex)
    {
        _logger.LogError("ProcessSomeOtherDifferentQuery ran into an error : " + ex);
    }
}

The first query (ProcessSomeQuery) dependency injects IMyDataBaseContext. Note this is NOT entity framework. It is registered with Autofac which defaults to instance per dependency (transient). It just retrieves some data.

private readonly IMyDataBaseContext _myDatabaseContext;
private readonly ILogger<ProcessSomeQueryHandler> _logger;

public ProcessSomeQueryHandler(IMyDataBaseContext myDatabaseContext, ILogger<ProcessSomeQueryHandler> logger)
{
        _myDatabaseContext = myDataBaseContext;
        _logger = logger;
}

The second query (ProcessSomeOtherDifferentQuery) only injects logger, nothing else. The handle also doesn’t do much other than log a message.

private readonly ILogger<ProcessSomeOtherDifferentQueryHandler> _logger;

public ProcessSomeOtherDifferentQueryHandler(ILogger<ProcessSomeOtherDifferentQueryHandler> logger)
{
  _logger = logger;
}

Some weird behaviors

  • Only the second mediator call results in an error. If I swap the order of ProcessSomeQuery and ProcessSomeOtherDifferentQuery, such that ProcessSomeOtherDifferentQuery is now the first mediator call and ProcessSomeQuery is now the second mediator call, then ProcessSomeQuery will fail now.

  • If I replace ProcessSomeOtherDifferentQuery with ProcessSomeQuery such that I have two mediator calls to ProcessSomeQuery, it will still give me the same error on the second call.

  • Notice that only ProcessSomeQuery dependency injects the database context. ProcessSomeOtherDifferentQuery only injects the logger. This results in the error. If both depdency injects the database context, still only the second mediator call fails.

  • I have tried changing the scopes in all sorts of configurations, no luck unfortuantely.

  • If I remove the database context injection from ProcessSomeQuery, then it results in NO error.

All my registrations (these are actually in different files)

services.AddTransient<IMainLogic, MainLogic>();
services.AddSingleton<IDateTimeProvider, DateTimeProvider>();
IMediator is registered with Autofac which defaults to Transient, I believe
MyDataBaseContext is registered with autofac which defaults to Transient

How can I ensure both mediator calls succeed without running into the ObjectDisposedException?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật