Are you right that GetCustomAttributes does not allow custom attributes through Autofac - if you think about it, how does FCL code like GetCustomAttributes know about Autofac? Custom attributes are actually extracted from the assembly metadata, so they never go through the Autofac resolution process, so your registration code is never used.
What you can do is enter the services into the attribute instance yourself. Start with the code in the Oliver answer to create a list of aspect attributes. However, before returning the list, you can process each attribute and enter services in any dependent fields and properties. I have a class called AttributedDependencyInjector , which I use with the extension method. It uses reflection to scan fields and properties decorated with InjectDependencyAttribute , and then sets the value of these properties. There is quite a lot of code to deal with various scenarios, but here it is.
Attribute Class:
Injector Class:
public class AttributedDependencyInjector {
Extension Method:
public static class RegistrationExtensions {
source share