MediaConversions can contain null values

From version 4.4.20, the MediaConversions property of the MediaItem class can contain null entries.

The MediaConversions property contains all requested conversions for a media item. The property is filled with all requested formats that the media item can be converted into.

Example

var client = ClientFactory.GetSdkClient();

//request a series of format for the media items
var imageFormat = new ImageFormat {Effects = {new ResizeEffect(200, 200)}};
var thumbnailFormat = new ThumbnailFormat {Effects = {new ResizeEffect(200, 200)}};
var webMediaFormat = new WebMediaFormat {Effects = {new ResizeEffect(200, 200)}};
var originalFormat = new OriginalFormat();
var query = new MediaItemQuery
{
    Populate = {MediaFormats = {imageFormat, thumbnailFormat, webMediaFormat, originalFormat}},
    Filter = {Take = 20}
};

var mediaService = client.CreateChannel<IMediaService>();
var mediaItems = mediaService.Find(query);

In ImageVault prior to 4.4.20, the order of this list is not determined. It can differ between requests.

In ImageVault 4.4.20 and later, the order of this list follows the same order as the supplied format list. This also means that if a specific media item cannot be converted to a requested format, this post on the list is null.

foreach (var mediaItem in mediaItems) {
    Console.WriteLine(@"MediaItem: {0}", mediaItem.Id);
    foreach (var mediaConversion in mediaItem.MediaConversions) {
        //null values are present for those media that cannot be converted for a specific format.
        if (mediaConversion == null) continue;
        Console.WriteLine(@"Converted to format {0} with url {1}",
            mediaConversion.MediaFormatId, mediaConversion.Url);
    }
}

This might be a potential issue for your code if you directly access the MediaConversions list and don't check for null values.

Workaround

The recommended workaround is to modify your code so that you check for null values.

If that isn't an option, you can supply the following configuration directive to the ImageVault.Core.Host.exe.config file to exclude these null elements.

Add an appSettings key named ExcludeNullMediaConversionsInMediaQueries and set its value to true.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="imagevault.core" type="ImageVault.Core.Configuration.CoreConfigurationSection,ImageVault.Core" />
  </configSections>
  <imagevault.core>
    <appSettings>
      <add key="ExcludeNullMediaConversionsInMediaQueries" value="true"/>
    </appSettings>
  </imagevault.core>
</configuration>
comments powered by Disqus