Mike Masey
12/17/2024, 12:38 PMprolog
[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.Mike Masey
12/17/2024, 12:38 PMusing (ExecutionContext.SuppressFlow())
but nothing seems to do the job.
- https://github.com/umbraco/Umbraco-CMS/issues/13804
- https://discord-chats.umbraco.com/t/18810092/ambientcontext-error-when-trying-to-use-conentservice-within
- https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/
Has anyone found a solution for this? Unfortunately it'll be a bit tricky for me to share the actual codeMike Masey
12/17/2024, 12:40 PMNik
12/17/2024, 2:19 PMNik
12/17/2024, 2:22 PMMike Masey
12/17/2024, 5:33 PMcs
using var backgroundScope = new BackgroundScope(_serverMessenger);
using var umbracoContext = _contextFactory.EnsureUmbracoContext();
using var serviceScope = _serviceProvider.CreateScope();
kdx-perbol
12/17/2024, 7:54 PMMike Masey
12/18/2024, 12:35 PMnzdev
12/19/2024, 12:55 AMMike Masey
12/19/2024, 9:32 AMBalázs Kerper
03/03/2025, 9:25 AMcsharp
public async Task Run(PerformContext? performContext, CancellationToken cancellationToken)
{
performContext.WriteLine("Starting the process");
Task someWorkTask;
using (ExecutionContext.SuppressFlow())
{
someWorkTask = Task.Run(async () =>
{
await _someService.SomeWorkAsync();
}, cancellationToken);
}
await someWorkTask;
performContext.WriteLine("Finishing the process");
}
the PerformContext is not super important obviously, but the position if the someWorkTask awaiting it is, otherwise hangfire would just fire and forget, but you'd have no control over it. It is quite a recent change, but it is in production now for a week, and we've not seen the scope issues since.
Everything the others has written above also stands and we are also using them (BackgroundScope, contextfactory, serviceprovider etc.)kdx-perbol
03/03/2025, 9:33 AM