Autofac Resolve <IEnumerable <T>> () returns an empty list

I read a lot of Autofac / wikis docs that indicate that I can get a list of all registered types by doing something simple, as shown below:

var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .AssignableTo<IPersistedModel>(); var container = builder.Build(); var allTypes = container.Resolve<IEnumerable<IPersistedModel>>(); 

The allTypes problem appears blank.

In the debugger, I see inside the container, and there are 7 suitable types and even an IPersistedModel array ... but they do not return to Resolve ().

What am I missing?

+4
source share
1 answer

You need to register the types as an interface using .As<IPersistedModel>() or for all your interfaces using .AsImplementedInterfaces() :

 builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .AssignableTo<IPersistedModel>() .AsImplementedInterfaces(); 
+6
source

Source: https://habr.com/ru/post/1390766/


All Articles