I'm currently upgrading Sitecore to 8.2 update 7 from 8.0 and have come across an issue with some custom code that overrides the GetMediaUrl of the MediaProvider class. It states that MediaProvider.MediaProvider() is obsolete. Does anyone know of a solution for this or has anyone overridden this method in 8.2 update 7?

public class MediaProvider : Sitecore.Resources.Media.MediaProvider
    public override string GetMediaUrl(MediaItem item, MediaUrlOptions options)
        Assert.ArgumentNotNull(item, "item");

        var url = base.GetMediaUrl(item, options);
        if (url.ToLowerInvariant().Contains("/-/media"))
            var revision = item.InnerItem.Statistics.Revision;

            ID revisionId;
            if (ID.TryParse(item.InnerItem.Statistics.Revision, out revisionId))
                revision = revisionId.ToShortID().ToString().ToLowerInvariant();

            var revisionUrl = new UrlString(url);
            revisionUrl.Add("sc_revision", revision);
            return HashingUtils.ProtectAssetUrl(revisionUrl.ToString());
        return HashingUtils.ProtectAssetUrl(url);

If you look at the code for the MediaProvider you will notice that only the parameterless constructor is marked as obsolete:

/// <summary>
/// Initializes a new instance of the <see cref="T:Sitecore.Resources.Media.MediaProvider" /> class.
/// </summary>
public MediaProvider()
  : this(ServiceLocator.ServiceProvider.GetRequiredService<BaseFactory>())

This is because, in later versions of Sitecore, the MediaProvider can be created using the container that will inject the BaseFactory object into the constructor that is not obsolete.

public MediaProvider(BaseFactory factory)
    // omitted for brevity...

So its not entire MediaProvider class that is obsolete and you are fine to continue to override that GetMediaUrl method in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.