AbpBootstrapper.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Reflection;
  3. using Abp.Auditing;
  4. using Abp.Authorization;
  5. using Abp.Configuration.Startup;
  6. using Abp.Dependency;
  7. using Abp.Dependency.Installers;
  8. using Abp.Domain.Uow;
  9. using Abp.EntityHistory;
  10. using Abp.Modules;
  11. using Abp.PlugIns;
  12. using Abp.Runtime.Validation.Interception;
  13. using Castle.Core.Logging;
  14. using Castle.MicroKernel.Registration;
  15. using JetBrains.Annotations;
  16. namespace Abp
  17. {
  18. /// <summary>
  19. /// This is the main class that is responsible to start entire ABP system.
  20. /// Prepares dependency injection and registers core components needed for startup.
  21. /// It must be instantiated and initialized (see <see cref="Initialize"/>) first in an application.
  22. /// </summary>
  23. public class AbpBootstrapper : IDisposable
  24. {
  25. /// <summary>
  26. /// Get the startup module of the application which depends on other used modules.
  27. /// </summary>
  28. public Type StartupModule { get; }
  29. /// <summary>
  30. /// A list of plug in folders.
  31. /// </summary>
  32. public PlugInSourceList PlugInSources { get; }
  33. /// <summary>
  34. /// Gets IIocManager object used by this class.
  35. /// </summary>
  36. public IIocManager IocManager { get; }
  37. /// <summary>
  38. /// Is this object disposed before?
  39. /// </summary>
  40. protected bool IsDisposed;
  41. private AbpModuleManager _moduleManager;
  42. private ILogger _logger;
  43. /// <summary>
  44. /// Creates a new <see cref="AbpBootstrapper"/> instance.
  45. /// </summary>
  46. /// <param name="startupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</param>
  47. /// <param name="optionsAction">An action to set options</param>
  48. private AbpBootstrapper([NotNull] Type startupModule, [CanBeNull] Action<AbpBootstrapperOptions> optionsAction = null)
  49. {
  50. Check.NotNull(startupModule, nameof(startupModule));
  51. var options = new AbpBootstrapperOptions();
  52. optionsAction?.Invoke(options);
  53. if (!typeof(AbpModule).GetTypeInfo().IsAssignableFrom(startupModule))
  54. {
  55. throw new ArgumentException($"{nameof(startupModule)} should be derived from {nameof(AbpModule)}.");
  56. }
  57. StartupModule = startupModule;
  58. IocManager = options.IocManager;
  59. PlugInSources = options.PlugInSources;
  60. _logger = NullLogger.Instance;
  61. if (!options.DisableAllInterceptors)
  62. {
  63. AddInterceptorRegistrars();
  64. }
  65. }
  66. /// <summary>
  67. /// Creates a new <see cref="AbpBootstrapper"/> instance.
  68. /// </summary>
  69. /// <typeparam name="TStartupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</typeparam>
  70. /// <param name="optionsAction">An action to set options</param>
  71. public static AbpBootstrapper Create<TStartupModule>([CanBeNull] Action<AbpBootstrapperOptions> optionsAction = null)
  72. where TStartupModule : AbpModule
  73. {
  74. return new AbpBootstrapper(typeof(TStartupModule), optionsAction);
  75. }
  76. /// <summary>
  77. /// Creates a new <see cref="AbpBootstrapper"/> instance.
  78. /// </summary>
  79. /// <param name="startupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</param>
  80. /// <param name="optionsAction">An action to set options</param>
  81. public static AbpBootstrapper Create([NotNull] Type startupModule, [CanBeNull] Action<AbpBootstrapperOptions> optionsAction = null)
  82. {
  83. return new AbpBootstrapper(startupModule, optionsAction);
  84. }
  85. /// <summary>
  86. /// Creates a new <see cref="AbpBootstrapper"/> instance.
  87. /// </summary>
  88. /// <typeparam name="TStartupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</typeparam>
  89. /// <param name="iocManager">IIocManager that is used to bootstrap the ABP system</param>
  90. [Obsolete("Use overload with parameter type: Action<AbpBootstrapperOptions> optionsAction")]
  91. public static AbpBootstrapper Create<TStartupModule>([NotNull] IIocManager iocManager)
  92. where TStartupModule : AbpModule
  93. {
  94. return new AbpBootstrapper(typeof(TStartupModule), options =>
  95. {
  96. options.IocManager = iocManager;
  97. });
  98. }
  99. /// <summary>
  100. /// Creates a new <see cref="AbpBootstrapper"/> instance.
  101. /// </summary>
  102. /// <param name="startupModule">Startup module of the application which depends on other used modules. Should be derived from <see cref="AbpModule"/>.</param>
  103. /// <param name="iocManager">IIocManager that is used to bootstrap the ABP system</param>
  104. [Obsolete("Use overload with parameter type: Action<AbpBootstrapperOptions> optionsAction")]
  105. public static AbpBootstrapper Create([NotNull] Type startupModule, [NotNull] IIocManager iocManager)
  106. {
  107. return new AbpBootstrapper(startupModule, options =>
  108. {
  109. options.IocManager = iocManager;
  110. });
  111. }
  112. private void AddInterceptorRegistrars()
  113. {
  114. ValidationInterceptorRegistrar.Initialize(IocManager);
  115. AuditingInterceptorRegistrar.Initialize(IocManager);
  116. EntityHistoryInterceptorRegistrar.Initialize(IocManager);
  117. UnitOfWorkRegistrar.Initialize(IocManager);
  118. AuthorizationInterceptorRegistrar.Initialize(IocManager);
  119. }
  120. /// <summary>
  121. /// Initializes the ABP system.
  122. /// </summary>
  123. public virtual void Initialize()
  124. {
  125. ResolveLogger();
  126. try
  127. {
  128. RegisterBootstrapper();
  129. IocManager.IocContainer.Install(new AbpCoreInstaller());
  130. IocManager.Resolve<AbpPlugInManager>().PlugInSources.AddRange(PlugInSources);
  131. IocManager.Resolve<AbpStartupConfiguration>().Initialize();
  132. _moduleManager = IocManager.Resolve<AbpModuleManager>();
  133. _moduleManager.Initialize(StartupModule);
  134. _moduleManager.StartModules();
  135. }
  136. catch (Exception ex)
  137. {
  138. _logger.Fatal(ex.ToString(), ex);
  139. throw;
  140. }
  141. }
  142. private void ResolveLogger()
  143. {
  144. if (IocManager.IsRegistered<ILoggerFactory>())
  145. {
  146. _logger = IocManager.Resolve<ILoggerFactory>().Create(typeof(AbpBootstrapper));
  147. }
  148. }
  149. private void RegisterBootstrapper()
  150. {
  151. if (!IocManager.IsRegistered<AbpBootstrapper>())
  152. {
  153. IocManager.IocContainer.Register(
  154. Component.For<AbpBootstrapper>().Instance(this)
  155. );
  156. }
  157. }
  158. /// <summary>
  159. /// Disposes the ABP system.
  160. /// </summary>
  161. public virtual void Dispose()
  162. {
  163. if (IsDisposed)
  164. {
  165. return;
  166. }
  167. IsDisposed = true;
  168. _moduleManager?.ShutdownModules();
  169. }
  170. }
  171. }