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
andProcessSomeOtherDifferentQuery
, such thatProcessSomeOtherDifferentQuery
is now the first mediator call andProcessSomeQuery
is now the second mediator call, thenProcessSomeQuery
will fail now. -
If I replace
ProcessSomeOtherDifferentQuery
withProcessSomeQuery
such that I have two mediator calls toProcessSomeQuery
, 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?