Media relation lost when programmatically setting media on nested block lists

We’re using Umbraco 13 (Cloud) and programmatically setting images on nested block list items via the MediaService. This works correctly in our local environment. However, when we sync content to the live environment using the transfer queue, the media relation is lost.

Interestingly, if we manually set the image via the Umbraco backoffice, the transfer to the live environment works as expected.

We’ve compared the resulting JSON between programmatically and manually set images, and they appear identical — including distinct id and mediaKey values.

What could be causing the programmatically set media references to break during content transfer?

Do you have a code snippit on how you set the media in code? And when do you do this?, in a task? Or on boot? Can you tell a bit more about the functionality?

This is the function we use to save an image:

        private Guid AddToMediaLibrary(MemoryStream stream, string fileName)
        {
            var folder = _mediaService.GetRootMedia().FirstOrDefault(f => f.Name == "Sprekers");

            if (folder == null)
            {
                folder = _mediaService.CreateMedia("Sprekers", -1, MediaTypes.Folder);
                _mediaService.Save(folder);
            }

            var image = _mediaService.GetPagedChildren(folder.Id, 0, int.MaxValue, out long total).FirstOrDefault(x => x.Name == fileName);

            if (image == null)
                image = _mediaService.CreateMediaWithIdentity(fileName, folder.Id, MediaTypes.Image);

            image.SetValue(_mediaFileManager, _mediaUrlGeneratorCollection, _shortStringHelper, _contentTypeBaseServiceProvider, Constants.Conventions.Media.File, fileName, stream);
            _mediaService.Save(image);

            return image.Key;
        }

We then set the nested content value like this:

var information = JsonConvert.SerializeObject(new InformationRoot()
{
    settingsData = new List<object>(),
    layout = new Layout() { UmbracoBlockList = new List<UmbracoBlockList>() { new UmbracoBlockList() { contentUdi = udi } } },
    contentData = new List<InformationContent>()
    {
        new InformationContent()
        {
            contentTypeKey = speakerContentTypeKey,
            udi = udi,
            FirstName = speaker.FirstName.Trim(),
            LastName = speaker.LastName.Trim(),
            Gender = speaker.Gender != null ? [speaker.Gender.Trim().ToLower().ToFirstUpper()] : Array.Empty<string>(),
            SpeakerLanguage = speaker.SpeaksLanguages != null ? speaker.SpeaksLanguages.ToArray() : Array.Empty<string>(),
            SpeakerServices = speaker.Services != null ? speaker.Services.Select(x => MapService(x.Slug!)).ToArray() : Array.Empty<string>(),
            Price = speaker.Price?.ToString() ?? string.Empty,
            Themes = themes ?? string.Empty,
            Expertises = expertises ?? string.Empty,
            Image = imageId.HasValue
                ? new List<ImageContent>()
                {
                    new()
                    {
                        Key = new GuidUdi("media", Guid.NewGuid()).Guid,
                        MediaKey = imageId
                    }
                }
                : []
        }
    }
});

content.SetValue("Information",
    information,
    culture);

@Luuk I just tested by setting an image on root level and then transferring it, and this works as expected. However, when we set the image in a block list it does not work. Would you have any idea why this behaviour occurs? Or is this a bug

I have no idea unfortunately… the reason why I asked about the context, is that during certain migrations - like package migrations - notifications get suppressed and uSync doesnt update. But that doesnt swwm to be the case here.