Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1444 posts 1855 karma points
    May 30, 2022 @ 12:14
    Gordon Saxby
    0

    Wasn't possible to a get a valid Snapshot

    I am getting this error when trying to access content that I have found / retrieved within a global function.. The code is

    using Microsoft.Extensions.DependencyInjection;
    using MyWebsite.Config.Interfaces;
    using MyWebsite.Models;
    using Umbraco.Cms.Core;
    using Umbraco.Cms.Core.Models.PublishedContent;
    using Umbraco.Cms.Core.Services;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Extensions;
    
    namespace MyWebsite.Config
    {
        public class GlobalConfiguration : IGlobalConfiguration
        {
            #region Private Properties
    
            private readonly IServiceProvider _serviceProvider;
            private readonly ServiceContext _serviceContext;
            private readonly IUmbracoContextFactory _umbracoContextFactory;
            private readonly IVariationContextAccessor _variationContextAccessor;
    
            #endregion
    
            #region Constructors
    
            public GlobalConfiguration(
                IServiceProvider serviceProvider,
                ServiceContext serviceContext,
                IUmbracoContextFactory umbracoContextFactory,
                IVariationContextAccessor variationContextAccessor
                )
            {
                _serviceProvider = serviceProvider;
                _serviceContext = serviceContext;
                _umbracoContextFactory = umbracoContextFactory;
                _variationContextAccessor = variationContextAccessor;
            }
    
            #endregion
    
            #region Email Template Methods
    
            public EmailChildTemplate? GetEmailChildTemplate(string templateAlias)
            {
                var currentCulture = Thread.CurrentThread.CurrentCulture;
                _variationContextAccessor.VariationContext = new VariationContext(currentCulture.Name);
    
                using var _ = _umbracoContextFactory.EnsureUmbracoContext();
                using var serviceScope = _serviceProvider.CreateScope();
    
                var query = serviceScope.ServiceProvider.GetRequiredService<IPublishedContentQuery>();
    
                var configSection = query.ContentAtRoot().First(t => t.IsDocumentType("configuration"));
                var emailTemplates = configSection.ChildrenForAllCultures.First(t => t.IsDocumentType("emailTemplates"));
    
                EmailChildTemplate? childTemplate = null;
                foreach (var emailTemplate in emailTemplates.ChildrenForAllCultures)
                {
                    var myTemplate = emailTemplate.ChildrenForAllCultures.FirstOrDefault(t =>
                        t.GetProperty("emailTemplateAlias").GetValue().ToString() == templateAlias);
    
                    if (myTemplate == null) continue;
    
                    childTemplate = new EmailChildTemplate(myTemplate, new PublishedValueFallback(_serviceContext, _variationContextAccessor));
                    break;
                }
    
                return childTemplate;
            }
    
            #endregion
        }
    }
    

    This seems to work and returns a valid "EmailChildTemplate".

    However, where I am using it fails with the error "Wasn't possible to a get a valid Snapshot"

    EmailChildTemplate? childTemplate = _globalConfiguration.GetEmailChildTemplate(templateAlias);
    if (childTemplate == null) return null;
    
    var rtnEmail = new EmailFields
    {
        Send = true,
        SenderName = childTemplate.FromEmailName ?? string.Empty, <<== Fails here
        SenderEmail = childTemplate.FromEmailAddress ?? string.Empty,
        ReceiverEmail = childTemplate.ReceiverEmailAddress ?? string.Empty,
        Subject = childTemplate.SubjectLine ?? string.Empty,
        BccEmail = childTemplate.BCcemailAddress ?? string.Empty,
        CcEmail = childTemplate.CCemailAddress ?? string.Empty,
        PreHeader = childTemplate.Preheader ?? string.Empty,
        Headline = childTemplate.Headline ?? string.Empty
    };
    

    What am I doing wrong?

  • Patrick de Mooij 72 posts 622 karma points MVP 3x c-trib
    May 30, 2022 @ 12:42
    Patrick de Mooij
    101

    I think this is caused because you aren't using the EnsureUmbracoContext in the place where you are using the fields. Therefore, the snapshot has probably been disposed already and cannot be found.

    Could you try putting using var _ = _umbracoContextFactory.EnsureUmbracoContext(); before the var rtnEmail = new EmailFields?

  • Gordon Saxby 1444 posts 1855 karma points
    May 30, 2022 @ 13:39
    Gordon Saxby
    0

    Yes, that fixed it.

    I think I wasn't understanding quite what was going on here!! 🙄

Please Sign in or register to post replies

Write your reply to:

Draft