Copied to clipboard

Flag this post as spam?

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


  • Daniel Rogers 134 posts 712 karma points
    Apr 15, 2021 @ 14:05
    Daniel Rogers
    0

    Using IContentService inside a UmbracoDefaultOwinStartup Hangfire scheduler

    Note I are using umbraco 8.12.2.

    I have successfully created a Hangfire scheduler that crawls another website for price information.

    What I need to do now is store that information on the correct doctype page.

    doctype is IBDeCommerce_Product can read these pages and get the old data using

    var umbracoContextFactory = Current.Factory.GetInstance<IUmbracoContextFactory>();
                using (var contextReference = umbracoContextFactory.EnsureUmbracoContext())
                {
                    var umbracoContext = contextReference.UmbracoContext;
    
                    var content = (IPublishedContent)umbracoContext.Content.GetAtRoot().FirstOrDefault();
    
    .... navagate the decendents get relivant info as needed 
    ProductPriceUpdate(productUdi, Product.ProductPrice);
            }
    
            private void ProductPriceUpdate(GuidUdi productUdi, IBDPrice Price)
            {
                IContentService sender = (IContentService)DependencyResolver.Current.GetService<IUmbracoContextFactory>().EnsureUmbracoContext().UmbracoContext;
                IContent content = sender.GetById(productUdi.Guid);
                content.SetValue("ProductPrice", Price);
                sender.SaveAndPublish(content);
            }
    

    I can load the new values into where I need them to go then I call the ProductPriceUpdate method to re publish the content.

    I think Im half way to the answer but I need to access the IConentService correctly as this line is where everything falls over.

            IContentService sender = (IContentService)DependencyResolver.Current.GetService<IUmbracoContextFactory>().EnsureUmbracoContext().UmbracoContext;
    

    log error as below. Which makes sense but how do I declear a IContentService so it works.

    {"@t":"2021-04-15T13:00:13.8581004Z","@mt":"Failed to process the job '13': an exception occurred. Retry attempt 1 of 10 will be performed in 00:00:16.","@l":"Warning","@x":"System.InvalidCastException: Unable to cast object of type 'Umbraco.Web.UmbracoContext' to type 'Umbraco.Core.Services.IContentService'.\r\n at Cultiv.Hangfire.ScheduleHangfireJobs.ProductPriceUpdate(GuidUdi productUdi, IBDPrice Price) in D:\Web Development\Intelligence By Design 2021\IBDeCommerce\Helpers\IBDeCommerceDashboardHelpers.cs:line 172\r\n at Cultiv.Hangfire.ScheduleHangfireJobs.GetProductDetailsByXPath(IbdeCommerceProduct Product, IbdeCommerceBrand Brand) in D:\Web Development\Intelligence By Design 2021\IBDeCommerce\Helpers\IBDeCommerceDashboardHelpers.cs:line 167\r\n at Cultiv.Hangfire.ScheduleHangfireJobs.DoIt(PerformContext context, Int32 shopId, Int32 brandId) in D:\Web Development\Intelligence By Design 2021\IBDeCommerce\Helpers\IBDeCommerceDashboardHelpers.cs:line 108","SourceContext":"Hangfire.AutomaticRetryAttribute","ProcessId":31876,"ProcessName":"iisexpress","ThreadId":23,"AppDomainId":2,"AppDomainAppId":"LMW3SVC2ROOT","MachineName":"DESKTOP-DCF20OR","Log4NetLevel":"WARN "}

  • Alex Skrypnyk 6134 posts 23953 karma points MVP 7x admin c-trib
    Apr 15, 2021 @ 17:00
    Alex Skrypnyk
    1

    Use Dependency resolver like this:

    IContentService sender = DependencyResolver.Current.GetService<IContentService>();
    
Please Sign in or register to post replies

Write your reply to:

Draft