Lets say I have EmailService which implments IEmailService. And EmailService has constructor dependency on ILoggingService. Now given that I have several implementations of ILoggingService, can achieve something like this:
<component service="IEmailService, MyInterfaces" type="EmailService, MyLib">
<parameters>
<parameter name="loggingService" value="LoggingService, MyLib" />
</parameters>
</component>
I have looked at giving names to registered types but so far couldn’t find an example of how to use them from XML configuration.
In short I want to use XML configuration to specify which concrete logger implementation gets injection.
XML configuration in Autofac is targeted more toward the 80% use case rather than being a full implementation of Autofac’s flexibility in XML form. Autofac instead recommends using its module mechanism for configuration. Modules, coupled with the XML configuration, can be a very powerful way to achieve what you’re looking to accomplish and still have that flexibility to switch between dependencies as needed.
First, create an Autofac module that does the registration you want:
Notice it’s a little more complex of a registration because you’re requiring a very specific resolution.
Now in XML configuration, register that module:
When you want to switch configurations, register a different module rather than fiddling with specific component registries.
Code disclaimer: I’m writing the syntax from memory and I’m not a compiler, so you may have to do a little tweaking… but the premise holds. Isolate the complexity in a module, then register your module.