0

I have a dependency which provides a number of services using its static ServiceManager. It also provides a list of available types.

Type[] ServiceManager.GetServiceTypes();
object GetService(Type t);

In an Autofac Module, I'd like to enumerate these types and register 'dynamic instantiation' of them. It's important that I call ServiceManager.GetService each time an instance is requested.

1 Answer 1

0

I ended up using my own RegistrationBuilder, looks pretty funky but it works. Have I missed an obvious trick?

        foreach (var type in ServiceManager.GetServiceTypes())
        {
            var rb = RegistrationBuilder.ForDelegate(
                type, 
                (ctx, parms) => ServiceManager.GetService(type))
                .ExternallyOwned();

            builder.RegisterCallback(
                cr => RegistrationBuilder.RegisterSingleComponent(cr, rb));
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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