在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
var builder= new ContainerBuilder(); //注册当前程序集中的所有controller;加上PropertiesAutowired()后,再可以使用属性注入 builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired(); Assembly asmService= Assembly.Load("TestService"); //在bulider中获取不是抽象类的相关程序集;这块添加PropertiesAutowired()可以让接口调用其他接口, //实现接口间属性注入 builder.RegisterAssemblyTypes(asmService) .Where(t => !t.IsAbstract).AsImplementedInterfaces().PropertiesAutowired(); var container=builder.Build(); //注册在DepandencyResolver中 DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
ICityService cityService =DependencyResolver.Current.GetService<ICityService>() 如果在quartz等单独的线程中,无法通过DependencyResolver.Current.GetService<ICityService>()获取,可以使用 var container = AutofacDependencyResolver.Current.ApplicationContainer; using (container.BeginLifetimeScope()) { cityService = container.Resolve<ICityService>(); }
|
请发表评论