Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Elgars 5 posts 75 karma points
    Oct 11, 2021 @ 08:56
    Elgars
    0

    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 :-)

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 01, 2021 @ 23:19
    Markus Johansson
    0

    Hi!

    Did you get anywhere with solving this? Any ideas on what the issue is/was?

  • Markus Johansson 1936 posts 5864 karma points MVP 2x c-trib
    Dec 02, 2021 @ 00:18
    Markus Johansson
    0

    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.

  • Frederik Sørensen 6 posts 76 karma points
    Jan 09, 2023 @ 09:36
    Frederik Sørensen
    0

    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:

    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.

Please Sign in or register to post replies

Write your reply to:

Draft