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().

Any suggestions highly appreciated,

Thanks in advance :slight_smile:


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/107309-the-scope-being-disposed-is-not-the-ambient-scope-in-recurringhostedservicebase