ControllerModule.cs 750 B

1234567891011121314151617181920212223242526272829
  1. 
  2. using Autofac;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System.Linq;
  5. using System.Reflection;
  6. using Module = Autofac.Module;
  7. namespace Admin.Core.RegisterModules
  8. {
  9. public class ControllerModule : Module
  10. {
  11. /// <summary>
  12. /// 控制器注入
  13. /// </summary>
  14. public ControllerModule()
  15. {
  16. }
  17. protected override void Load(ContainerBuilder builder)
  18. {
  19. var controllerTypes = Assembly.GetExecutingAssembly().GetExportedTypes()
  20. .Where(type => typeof(ControllerBase).IsAssignableFrom(type))
  21. .ToArray();
  22. // 配置所有控制器均支持属性注入
  23. builder.RegisterTypes(controllerTypes).PropertiesAutowired();
  24. }
  25. }
  26. }