Owain Jones
10/11/2023, 11:36 AMcsharp
public virtual async Task<TOut> Update(TOut item)
{
using var scope = _scopeProvider.CreateScope();
using (var transaction = scope.Database.GetTransaction())
{
var poco = _mapper.Map<T>(item);
await scope.Database.UpdateAsync(poco);
transaction.Complete();
item = _mapper.Map<TOut>(poco);
}
scope.Complete();
return item;
}
The above class is registered as transient.
If I call this method multiple times in one request (e.g. in a foreach loop), then I seem to run into this exception:
The Scope being disposed {guid} is not the Ambient Scope {guid}
and all subsequent attempts to run the method result in No AmbientContext was found.
exceptions; only a restart fixes it until it happens again.
The addition of a Thread.Sleep(500);
to the start of the Update method seems to completely fix the issue. But that's a gross band aid fix which only masks whatever the underlying issue is. (feels like the first scope is not being closed in time and the second scope is created as a child scope?)
has anyone ran into this before? Am I missing something stupidly obvious?huwred
10/11/2023, 12:00 PMOwain Jones
10/11/2023, 6:14 PM