一、基本配置
1、通过配置的方式使用Autofac
<?xml version="1.0"?> <configuration> <configSections> <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/> </configSections> <autofac defaultAssembly="ConsoleApplication3"> <components> <component type="ConsoleApplication3.Worker, ConsoleApplication3" service="ConsoleApplication3.IPerson" /> </components> </autofac> </configuration>
2、通过RegisterModule方式使用配置文件中的信息
static void Main(string[] args) { ContainerBuilder builder = new ContainerBuilder(); builder.RegisterType<AutoFacManager>(); builder.RegisterModule(new ConfigurationSettingsReader("autofac")); using (IContainer container = builder.Build()) { AutoFacManager manager = container.Resolve<AutoFacManager>(); manager.Say(); } Console.ReadKey(); }
3、通过Register的方式
builder.RegisterModule(new ConfigurationSettingsReader("autofac")); builder.Register(c => new AutoFacManager(c.Resolve<IPerson>()));