question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hook/Extension point modifying IRegistrationBuilder

See original GitHub issue

(I’m not 100% sure that this isn’t possible today but afaik it isn’t…)

Problem Statement

We use our own little aop fw for our services. To use this in combination with autofac we use the same technique as eg autofac.extras.dynamicproxy does, having an extension method that replaces the implemented type of the service. It works fine but as we want this for all our services (replace registered service type with our generated one), it would be nice if there was any hook/extension point or similar in autofac that would be called for every registered component so we could skip this ext method call for each and every registration (many, many, many registrations in our system).

Desired Solution

Don’t know the inner working of autofac good enough to have a good suggestion. AttachToRegistrationSource and Registered event seem to happen “too late”. Would it be possible to inject something to ContainerBuilder that will be called for each registration maybe?

Alternatives You’ve Considered

We can create a composition for ContainerBuilder and extend its methods behavior there. Or we could create our own extension methods directly on ContainerBuilder. Still, would be great if a “registration hook” would be available in autofac itself, especially for us with a big legacy system with many different processes and containerbuilder instances.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:12 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
alistairjevanscommented, Oct 11, 2022

OK, so after much going round the houses on a ReplaceActivator method, we realised we didn’t need to do any of it, because this scenario already “just works” using resolve pipeline middleware.

If the desire from @RogerKratz is to replace the activator for all/some components, you can add middleware to do it. In the below example I create middleware that ignores the existing registered activator at resolve time, and uses its own. The middleware is added in the Registered method via the PipelineBuilding event.

public class ActivatorReplacementWithMiddleware
{
    [Fact]
    public void CanReplaceActivatorWithMiddleware()
    {
        var builder = new ContainerBuilder();

        builder.ComponentRegistryBuilder.Registered += (sender, args) =>
        {
            if (args.ComponentRegistration.Activator.LimitType == typeof(ServiceA))
            {
                args.ComponentRegistration.PipelineBuilding += (sender, pipeline) =>
                {
                    pipeline.Use(new ActivatorReplacementMiddleware(() => new ServiceA(createdCustom: true)));
                };
            }
        };

        builder.RegisterType<ServiceA>();

        var container = builder.Build();

        var instance = container.Resolve<ServiceA>();

        Assert.True(instance.CreatedCustom);
    }
}

class ActivatorReplacementMiddleware : IResolveMiddleware
{
    private readonly Func<object> _getInstance;

    public PipelinePhase Phase => PipelinePhase.Activation;

    public ActivatorReplacementMiddleware(Func<object> getInstance)
    {
        this._getInstance = getInstance;
    }

    public void Execute(ResolveRequestContext context, Action<ResolveRequestContext> next)
    {
        // If you want, you can call the existing activator by invoking next,
        // but you don't need to.
        // next(context);

        context.Instance = _getInstance();
    }
} 
1reaction
alistairjevanscommented, Sep 19, 2022

Yes, AttachToComponentRegistration is just attached directly to the Registered event, and has access to IComponentRegistration before the pipeline build happens.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Altering existing services, providing dynamic services
If a service provides extension capabilities with events, hooks, or by other means, that is definitely more compatible with other extensions.
Read more >
Extension point and extensions
An extension point interacts with unknown number of extensions. It needs to define one or more interfaces as the contracts that each extension...
Read more >
Hook into the Eclipse build process? - java
You can write an incremental project builder and register it via extension point: http://www.eclipse.org/articles/Article-Builders/builders.
Read more >
Proactively keep resources secure and compliant with ...
CloudFormation Hooks is a supported extension type in the AWS CloudFormation registry. The registry makes it easier to distribute and ...
Read more >
Extension Points defined in Jenkins Core
When Jenkins receives HTTP basic authentication, this hook will validate the username/password pair. Implementations: Jenkins Core: jenkins.security.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found