AppServiceAttribute.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. namespace Infrastructure.Attribute
  3. {
  4. /// <summary>
  5. /// 参考地址:https://www.cnblogs.com/kelelipeng/p/10643556.html
  6. /// 标记服务
  7. /// 如何使用?
  8. /// 1、如果服务是本身 直接在类上使用[AppService]
  9. /// 2、如果服务是接口 在类上使用 [AppService(ServiceType = typeof(实现接口))]
  10. /// </summary>
  11. [AttributeUsage(AttributeTargets.Class, Inherited = false)]
  12. public class AppServiceAttribute : System.Attribute
  13. {
  14. /// <summary>
  15. /// 服务声明周期
  16. /// 不给默认值的话注册的是AddSingleton
  17. /// </summary>
  18. public LifeTime ServiceLifetime { get; set; } = LifeTime.Scoped;
  19. /// <summary>
  20. /// 指定服务类型
  21. /// </summary>
  22. public Type ServiceType { get; set; }
  23. /// <summary>
  24. /// 是否可以从第一个接口获取服务类型
  25. /// </summary>
  26. public bool InterfaceServiceType { get; set; }
  27. }
  28. public enum LifeTime
  29. {
  30. Transient, Scoped, Singleton
  31. }
  32. }