Using IContentService within a Hangfire task occasionally throws 'No AmbientContext was found' error
m

Mike Masey

10 months ago
I have a hangfire task that loops through a bunch of data from an API, uses it to either Save, Save & Publish or Unpublish multiple content items. The ContentService itself seesm to be working as expected and is able to update each bit of content correctly, but I'm seeing the following error somewhat sporadically.
prolog
[12:30:03 ERR] Error occurred executing workItem.
System.AggregateException: One or more errors occurred. (No AmbientContext was found.)
 ---> System.InvalidOperationException: No AmbientContext was found.
   at Umbraco.Cms.Infrastructure.Scoping.AmbientScopeContextStack.Pop()
   at Umbraco.Cms.Infrastructure.Scoping.ScopeProvider.PopAmbientScopeContext()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.<>c__DisplayClass56_0.<RobustExit>g__HandleScopeContext|0()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.TryFinally(Action[] actions)
   --- End of inner exception stack trace ---
   at Umbraco.Cms.Infrastructure.Scoping.Scope.TryFinally(Action[] actions)
   at Umbraco.Cms.Infrastructure.Scoping.Scope.RobustExit(Boolean completed, Boolean onException)
   at Umbraco.Cms.Infrastructure.Scoping.Scope.DisposeLastScope()
   at Umbraco.Cms.Infrastructure.Scoping.Scope.Dispose()
   at Umbraco.Cms.Infrastructure.Examine.ExamineUmbracoIndexingHandler.DeferredReIndexForContent.<>c__DisplayClass6_0.<Execute>b__0(CancellationToken cancellationToken)
   at Umbraco.Cms.Infrastructure.HostedServices.QueuedHostedService.BackgroundProcessing(CancellationToken stoppingToken)
This error seems to occur more frequently on the first run, then less so afterwards, although it does still happen. For context, it's currently updated around 20 content items.