Calling all ISomething instances in Autofac

和自甴很熟 提交于 2020-01-04 02:44:09

问题


I have an interface ISomething with a method Start. I want to get all implementations of this interface (in multiple assemblies, the main one and all referenced ones) and call the Start method on application start. How can I do this with Autofac 2.4.4.705?


回答1:


you can resolve

 IEnumerable<ISomething>

and call Start for each of them

Forgot to mention, that you should first register all implementations of ISomething.

Assembly[] assemblies = ...;
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(assemblies).AssignableTo<ISomething>().As<ISomething>();
var container = builder.Build();

Where "assemblies" is an array of assemblies you want to register from.



来源:https://stackoverflow.com/questions/5074719/calling-all-isomething-instances-in-autofac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!