How to replace default implementation of ContentMediaResolver?

Vote:
 

In our site we have two definitions for how our ImageData content is supposed to be setup, one to be used for CMS-images and another one for our Commerce images. In CMS 11 the solution described here works like a charm: https://www.getadigital.com/blog/resolve-default-media-types-in-episerver. However I can't seem to find how to replace the existing implementation that is being setup like this:

I've tried to do the following in our startup.cs:

services.RemoveAll<ContentMediaResolver>();
services.AddSingleton<ContentMediaResolver, CustomContentMediaResolver>();

Also this:

var contentMediaResolver = new ServiceDescriptor(typeof(ContentMediaResolver), typeof(CustomContentMediaResolver), ServiceLifetime.Singleton);
services.Replace(contentMediaResolver);

After looking at a Foundation site I've also tried this in a IConfigurableModule:

public void ConfigureContainer(ServiceConfigurationContext context)
{
    context.ConfigurationComplete += (sender, args) =>
    {
        args.Services.Intercept<ContentMediaResolver>((provider, resolver) => new CustomContentMediaResolver());
    };
}

However none of these code changes seem to do any difference.

Anyone who knows how I can switch default implementation of ContentMediaResolver in Optimizely CMS 12?

Thanks in advance!
/Martin

#311301
Edited, Oct 23, 2023 9:42
Vote:
 

That is a good question. You've done right things, however in case of ContentMediaResolver, it was called in ModelSyncInitialization, as this is likely before any of your initialization modules. Your best bet is to do this in your IConfigurableModule.ConfigureContainer

var services = context.Services;

services.AddSingleton<ContentMediaResolver, CustomContentMediaResolver>();

#311306
Oct 23, 2023 11:13
Vote:
 

I've tried adding this piece of code (two different attempts in ConfigureContainer) to my solution but still no luck:

[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class InitializeSite : IConfigurableModule
{
    public void Initialize(InitializationEngine context)
    {
    }

    public void Uninitialize(InitializationEngine context)
    {
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        var contentMediaResolverDescriptor =
            new ServiceDescriptor(typeof(ContentMediaResolver), typeof(CustomContentMediaResolver),
                ServiceLifetime.Singleton);
        context.Services.Replace(contentMediaResolverDescriptor);

        // context.Services.AddSingleton<ContentMediaResolver, CustomContentMediaResolver>();
    }
}

Should I have some other type in ModuleDependency perhaps?

#311309
Oct 23, 2023 12:14
Vote:
 

context.Services.AddSingleton<ContentMediaResolver, CustomContentMediaResolver>(); should be enough ? Have you tried to uncomment it ?

No you don't need ModuleDependency - the default one is registered with the attribute so it'll be registered first . You need to register before ModelSyncInitialization, however that is not possible with IInitializationModule (you can only make sure your module is called after the builtin ones, not vice versa). IConfigurableModule.ConfigureContainer should be called before any initialization module, and that should just work 

#311312
Oct 23, 2023 12:30
Vote:
 

Some more information Register your custom implementation, the sure way – Quan Mai's blog (vimvq1987.com) (your case is a bit of exception) 

#311313
Oct 23, 2023 12:31
Vote:
 

Some more information Register your custom implementation, the sure way – Quan Mai's blog (vimvq1987.com) (your case is a bit of exception) 

#311314
Oct 23, 2023 12:31
Vote:
 

I've now verified that if I try fetch an instance of the ContentMediaResolver in for instance an implementation of ISelectionFactory, it is my custom implementation of the class that I get back. But still when uploading images in the CMS UI my CustomContentMediaResolver-class is not being hit and the image is created using the wrong content type. How can that be happening? 

#311321
Oct 23, 2023 13:25
Vote:
 

Strange, I can see that FileUploadController has a dependency on ContentMediaResolver, but it should resolve the correct instance.

Do you have some kind of custom DependencyResolver ?

#311323
Oct 23, 2023 13:46
Vote:
 

I've taken some time to see if I can reproduce the same problem in Foundation, and I believe I can do that.

If you look at this fork of Foundation (https://github.com/MEmanuelsson/OptimizelyFoundation) you can see what I've changed in this commit: https://github.com/episerver/Foundation/compare/main...MEmanuelsson:OptimizelyFoundation:main.

If I set a breakpoint in the HomeController I get an instance of my CustomContentMediaResolver

but if I add in image in the CMS I will not hit the CustomContentMediaResolver (if I add a breakpoint to that file) and also the image created is of the already existing ImageMediaData (named "Image file")

Can you see if I've done something wrong? Or is this a bug in the CMS?

/Martin

#311348
Oct 23, 2023 18:59
Vote:
 

Good investigation. Will get back to you when I find out

#311350
Oct 23, 2023 21:18
Vote:
 

Can confirm I can reproduce the issue. I reported bug CMS-30556 to the CMS UI team. Will get back when I hear from them

#311389
Oct 24, 2023 8:44
Vote:
 

Great, thanks a lot Quan!

Look forward to seeing a fix for this being released as soon as possible :-)

#311401
Oct 24, 2023 19:51
Vote:
 

Talked with a colleague in CMS UI team and he pointed out that only GetFirstMatchingContentType is used in FileUploadController. If you want to use your implementation in FileUploadController, then you have to override that, not GetFirstMatching 

#311483
Oct 26, 2023 8:23
Vote:
 

Switching to overriding GetFirstMatchingContentType does the trick, thank you Quan!

#311876
Nov 02, 2023 13:47
Vote:
 

Maybe this is something that should be added to the documentation https://docs.developers.optimizely.com/content-management-system/docs/media-types-and-templates, information about how to handle multiple types of ImageData for instance?

#311877
Nov 02, 2023 13:49
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.