Override Core Components

Hi there,

I’m hitting an error when saving members through a Hangfire task (already raised here Saving members without HttpContext throwing errors · Issue #8090 · umbraco/Umbraco-CMS · GitHub).

As a quick fix for the project I’m on I was looking for help in how to override core components?

Specifically I’m looking to have a new version of this to contain the fix - https://github.com/umbraco/Umbraco-CMS/blob/v8/contrib/src/Umbraco.Core/Compose/AuditEventsComponent.cs

Turns out there’s a handy Components.Replace function as part of the Composition class which allows you to swap out components. An example of how to use this would be:

using Umbraco.Core.Composing;
using Umbraco.Core;

namespace MyNamespace
{
    public class TestComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Replace<Umbraco.Core.Compose.AuditEventsComponent, TestComponent>();
        }
    }

    public class TestComponent : IComponent
    {
        public void Initialize()
        {
        }

        //...All the stuff I want to do instead of the AuditEventsComponent!

        public void Terminate()
        {
        }
    }
}

Thanks to Rowan for the help on Slack!

1 Like