Chriztian Steinmeier
01/08/2025, 8:51 AMNik
01/08/2025, 9:15 AMSebastian Dammark
01/08/2025, 11:30 AMContent Cache is null
).
My code looks like this:
public class PublishDesigns : INotificationHandler<UmbracoApplicationStartedNotification>
{
private readonly IContentService _contentService;
private readonly ILogger<PublishDesigns> _logger;
private readonly IUmbracoContextAccessor _context;
public PublishDesigns(IContentService contentService, ILogger<PublishDesigns> logger, IUmbracoContextAccessor context)
{
_contentService = contentService ?? throw new ArgumentNullException(nameof(contentService));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_context = context ?? throw new ArgumentNullException(nameof(context));
}
public void Handle(UmbracoApplicationStartedNotification notification)
{
if (_context.TryGetUmbracoContext(out IUmbracoContext? context) == false)
{
_logger.LogInformation("unable to get content");
}
if (context == null || context.Content == null)
{
_logger.LogInformation("Content Cache is null");
}
IPublishedContent? designFolder = null;
if (context != null && context.Content != null)
{
designFolder = context.Content.GetAtRoot().FirstOrDefault(x => x.ContentType.Alias == "USNStylesFolder");
}
if(designFolder != null) {
IEnumerable<IContent> designs = _contentService.GetPagedChildren(1711, 0, int.MaxValue, out long countDesigns) ?? Enumerable.Empty<IContent>();
foreach(var design in designs) {
_contentService.SaveAndPublish(design);
_logger.LogInformation($"Design({design.Id}) has been published");
}
}
}
}
Any suggestions how to get around this ?