How to get umbraco context using umbraco background runner?
Hi all.
Using Umbraco Cloud 8.5.3 - Locally
I am running a task with the Umbraco Background Runner https://our.umbraco.com/documentation/Reference/Scheduling/ where i save, saveandpublish and unpublish alot of content. Depending of its content. But its like its missing Umbraco context.
I get the same errors if i do it as a PerformRun or PerformRunAsync. (Just to clarify)
This is basically the Api Service
public class ApiService : IApiService
{
private readonly ILogger _logger;
private readonly IContentService _contentService;
private readonly IContentTypeService _contentTypeService;
public ApiService(ILogger logger, IContentService contentService, IContentTypeService contentTypeService)
{
_logger = logger;
_contentService = contentService;
_contentTypeService = contentTypeService;
}
// Alot of methods to Save, SaveAndPublish and UnPublish IContent.
}
This error is from a SaveAndPublish event.
at Umbraco.Web.Routing.RedirectTrackingComponent.StoreOldRoute(IContent entity, OldRoutesDictionary oldRoutes)
at Umbraco.Web.Routing.RedirectTrackingComponent.ContentService_Publishing(IContentService sender, PublishEventArgs`1 args)
at Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e)
at Umbraco.Core.Events.QueuingEventDispatcherBase.DispatchCancelable[TSender,TArgs](TypedEventHandler`2 eventHandler, TSender sender, TArgs args, String eventName)
at Umbraco.Core.Services.Implement.ContentService.StrategyCanPublish(IScope scope, IContent content, Boolean checkPath, IReadOnlyList`1 culturesPublishing, IReadOnlyCollection`1 culturesUnpublishing, EventMessages evtMsgs, ContentSavingEventArgs savingEventArgs, IReadOnlyCollection`1 allLangs)
at Umbraco.Core.Services.Implement.ContentService.CommitDocumentChangesInternal(IScope scope, IContent content, ContentSavingEventArgs saveEventArgs, IReadOnlyCollection`1 allLangs, Int32 userId, Boolean raiseEvents, Boolean branchOne, Boolean branchRoot)
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents)
at MyProject.Core.Services.ApiService.CreateOrUpdateActivity(IEnumerable`1 allExistingActivitiesInTemplate, ApiActivity apiActivity, IContent parentTemplate, ApiTemplate parentApiTemplate) i D:\Bitbucket\Xfiles\MyProject.Core\Services\ApiService.cs:line 613
at MyProject.Core.Services.ApiService.HandleData(ApiData data, Int32 activitiesPageId) i D:\Bitbucket\Xfiles\MyProject.Core\Services\ApiService.cs:line 215
at MyProject.Core.RecurringTasks.FullUpdateTask.PerformRunAsync(CancellationToken token) i D:\Bitbucket\Xfiles\MyProject.Core\RecurringTasks\FullUpdateTask.cs:line 82
If i disable the Umbraco Redirect Tracker, the error is gone, but then i am getting a new error.
System.InvalidOperationException: No current UmbracoContext at Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.GetHubAndHelper(IHubContext& hub, UmbracoHelper& helper)
at Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.ContentService_Published(IContentService sender, PublishEventArgs`1 e)
at Umbraco.Core.Events.TypedEventHandler`2.Invoke(TSender sender, TEventArgs e) at Umbraco.Core.Events.EventDefinition`2.RaiseEvent()
at Umbraco.Core.Events.QueuingEventDispatcher.ScopeExitCompleted()
at Umbraco.Core.Events.QueuingEventDispatcherBase.ScopeExit(Boolean completed) at Umbraco.Core.Scoping.Scope.<>c__DisplayClass71_0.<RobustExit>b__1() at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) at Umbraco.Core.Scoping.Scope.TryFinally(Int32 index, Action[] actions) at Umbraco.Core.Scoping.Scope.RobustExit(Boolean completed, Boolean onException)
at Umbraco.Core.Scoping.Scope.DisposeLastScope()
at Umbraco.Core.Scoping.Scope.Dispose()
at Umbraco.Core.Services.Implement.ContentService.SaveAndPublish(IContent content, String culture, Int32 userId, Boolean raiseEvents)
at MyProject.Core.Services.ApiService.CreateOrUpdateActivity(IEnumerable`1 allExistingActivitiesInTemplate, ApiActivity apiActivity, IContent parentTemplate, ApiTemplate parentApiTemplate) i D:\Bitbucket\Xfiles\MyProject.Core\Services\ApiService.cs:line 613 at MyProject.Core.Services.ApiService.HandleData(ApiData data, Int32 activitiesPageId) i D:\Bitbucket\Xfiles\MyProject.Core\Services\ApiService.cs:line 215 at MyProject.Core.RecurringTasks.FullUpdateTask.PerformRunAsync(CancellationToken token) i D:\Bitbucket\Xfiles\MyProject.Core\RecurringTasks\FullUpdateTask.cs:line 62
Now another weird thing is that its calling Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.ContentService_Published
All of it works if i call a webapi with the same code.
I have to confirm that this works. Actuali this is my working code. Independended call from Hangfire to unpublish passed events
UmbracoContext ctx = Umbraco.Web.UmbracoContext.Current;
if (ctx == null)
{
var dummyContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("/", string.Empty, new StringWriter())));
ctx = UmbracoContext.CreateContext(
dummyContext,
ApplicationContext.Current,
new WebSecurity(dummyContext, ApplicationContext.Current),
UmbracoConfig.For.UmbracoSettings(),
UrlProviderResolver.Current.Providers,
false);
}
var umbHelper = new UmbracoHelper(ctx);
var iPublishedContent = umbHelper.TypedContent(iContent.Id);
How to get umbraco context using umbraco background runner?
Hi all.
Using Umbraco Cloud 8.5.3 - Locally
I am running a task with the Umbraco Background Runner https://our.umbraco.com/documentation/Reference/Scheduling/ where i save, saveandpublish and unpublish alot of content. Depending of its content. But its like its missing Umbraco context.
I get the same errors if i do it as a PerformRun or PerformRunAsync. (Just to clarify)
This is basically the Api Service
This error is from a SaveAndPublish event.
If i disable the Umbraco Redirect Tracker, the error is gone, but then i am getting a new error.
Now another weird thing is that its calling Umbraco.Deploy.Cloud.LiveEditing.EventSubscriber.ContentService_Published
All of it works if i call a webapi with the same code.
I've had similar issues in the past (whilst using hangfire) and you have to 'fake' the umbraco context. You could try this (here's a link I found which is somewhat along the lines of how I did it: https://our.umbraco.com/forum/extending-umbraco-and-using-the-api/93790-access-umbracocontext-in-hangfire#comment-314887)
Although, I haven't tried the above. It should point you in the right direction if it doesn't work!
Hi Ryan.
Thanks for the reply, i will try giving that a shot.
Hi,
I have to confirm that this works. Actuali this is my working code. Independended call from Hangfire to unpublish passed events
Regards, /Asembly
is working on a reply...