The Scope being disposed is not the Ambient Scope in RecurringHostedServiceBase
Hello,
I've been trying to port some of V8 code to V9 and stumbled on exception regarding scope disposal, which is thrown only inside of my recurring hosted service.
Simplified example of how classes call each other and scope is being used:
public class ExampleHostedService : RecurringHostedServiceBase
{
private readonly IRuntimeState _runtimeState;
private readonly IServerRoleAccessor _serverRoleAccessor;
private readonly IExampleRepository _exampleRepository;
public override Task PerformExecuteAsync(object state) {
// Don't do anything if the site is not running.
if (_runtimeState.Level != RuntimeLevel.Run)
{
return Task.CompletedTask;
}
if (_serverRoleAccessor.CurrentServerRole == ServerRole.SchedulingPublisher ||
_serverRoleAccessor.CurrentServerRole == ServerRole.Single)
{
var items = _exampleRepository.FetchItems();
foreach(var item in items) {
// do something
item.UpdatedAt = DateTime.UtcNow;
_exampleRepository.Save(item);
}
}
return Task.CompletedTask;
}
}
public class ExampleRespository : IExampleRepository
{
private readonly IScopeProvider _scopeProvider;
private readonly IUmbracoMapper _mapper;
public IList<Item> FetchItems()
{
var result = new List<Item>();
using (var scope = _scopeProvider.CreateScope(autoComplete: true))
{
var items = scope.Database.Query<DbItem>().ToList();
result.AddRange(_mapper.MapEnumerable<DbItem, Item>(items));
}
return result;
}
public void Save(Item item)
{
using (var scope = _scopeProvider.CreateScope(autoComplete: true))
{
var itemToSave = _mapper.Map<DbItem>(item);
scope.Database.Save(itemToSave);
item.Id = itemToSave.Id;
}
}
}
Exception that is being thrown:
System.InvalidOperationException: The Scope
257381e0-e0e8-43e0-8459-0dc8ff4c466c being disposed is not the Ambient
Scope 2de2f904-39c4-4caf-b1ee-ca954b02ed8a. This typically indicates
that a child Scope was not disposed, or flowed to a child thread that
was not awaited, or concurrent threads are accessing the same Scope
(Ambient context) which is not supported. If using Task.Run (or
similar) as a fire and forget tasks or to run threads in parallel you
must suppress execution context flow with
ExecutionContext.SuppressFlow() and ExecutionContext.RestoreFlow().
Auto-completed scopes should be used for read-only operations ONLY. Do not use them if you do not understand the associated issues, such as the scope being completed even though an exception is thrown.
I could definitely be wrong about this and it could be a version specific issue.
I haven't had any issues in my v10 implementation where I complete the scope manually, with basically the same setup.
The Scope being disposed is not the Ambient Scope in RecurringHostedServiceBase
Hello,
I've been trying to port some of V8 code to V9 and stumbled on exception regarding scope disposal, which is thrown only inside of my recurring hosted service.
Simplified example of how classes call each other and scope is being used:
Exception that is being thrown:
Any suggestions highly appreciated,
Thanks in advance :-)
Hi!
Did you get anywhere with solving this? Any ideas on what the issue is/was?
I had a similar issue, or more like different random issues. Could be errors related to completed transactions, or that the Scope was disposed.
I was doing a "fire and forget" with Task.Run().
Turns out that adding
ExecutionContext.SuppressFlow();
Before kicking of the task seems to solve the problem for me.
Did you solve this?
If it's still relevant, then I think it could be because you're using the "autoComplete: true" when creating the scope in your "Save" method.
According to their v8 documentation:
I could definitely be wrong about this and it could be a version specific issue.
I haven't had any issues in my v10 implementation where I complete the scope manually, with basically the same setup.
is working on a reply...