Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 608 posts 2406 karma points
    Feb 06, 2020 @ 08:40
    Bo Jacobsen
    0

    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.

  • Ryan Helmn 26 posts 188 karma points
    Feb 10, 2020 @ 14:31
    Ryan Helmn
    0

    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!

  • Bo Jacobsen 608 posts 2406 karma points
    Feb 11, 2020 @ 07:31
    Bo Jacobsen
    0

    Hi Ryan.

    Thanks for the reply, i will try giving that a shot.

  • Asembli 86 posts 260 karma points
    Feb 12, 2020 @ 22:00
    Asembli
    0

    Hi,

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

    Regards, /Asembly

Please Sign in or register to post replies

Write your reply to:

Draft