Copied to clipboard

Flag this post as spam?

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


  • Andrei 15 posts 99 karma points
    Feb 17, 2023 @ 13:17
    Andrei
    0

    IPublishedContent.Url() throwing scope error when called in background running task (v10)

    I have a long running background task which I fire and forget with IBackgroundTaskQueue.QueueBackgroundWorkItem(token => Task.Run(()=>task))

    In the process, the task gathers some meta data from content nodes, thus calling IPublishedContent.Url() to get their url.

    As this is a long running task in the background I set it up with it's own scope like this:

    using(ExecutionContext.SuppressFlow()){
        using(_serviceScopeFactory.CreateAsyncScope()){
           // long running work here ..
        }
    }
    

    However, when trying to get the url for a node I get this:

    System.InvalidOperationException: The Scope 34e2e8a7-065e-4169-82ff-3f00f1751d62 being disposed is not the Ambient Scope 34e2e8a7-065e-4169-82ff-3f00f1751d62. 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().

    The error is thrown by LocalizationService:

       at Umbraco.Cms.Infrastructure.Scoping.Scope.Dispose()
       at Umbraco.Cms.Core.Services.LocalizationService.GetDefaultLanguageIsoCode()
    

    Any idea what am I missing here?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies