Application start

IServer responsible for monitoring the http request, forwarded to the HttpApplication, create httpcontext, process the request to release resources

Thus, Application module includes a register, register IServiceCollection services, the IServiceProvider dependency injection, and registered by the composition of the intermediate pipe startup, shut down and release operation.

Understand the relationship before Startup and abp module

1, services.AddApplication <DemoAppModule> () => DemoAppModule is abp module, which itself dependent on other modules, the dependency tree formed

2, where the use of autofac replacement, expansion method

 public static void UseAutofac(this AbpApplicationCreationOptions options)
        {
            var builder = new ContainerBuilder();
            options.Services.AddObjectAccessor(builder);
            options.Services.AddSingleton((IServiceProviderFactory<ContainerBuilder>) new AbpAutofacServiceProviderFactory(builder));
        }
   public static IHostBuilder UseAutofac(this IHostBuilder hostBuilder)
        {
            var containerBuilder = new ContainerBuilder();

            return hostBuilder.ConfigureServices((_, services) =>
                {
                    services.AddObjectAccessor(containerBuilder);
                })
                .UseServiceProviderFactory(new AbpAutofacServiceProviderFactory(containerBuilder));
        }

ABP Modular

public abstract class AbpModule : 
        IAbpModule,
        IOnPreApplicationInitialization,
        IOnApplicationInitialization,
        IOnPostApplicationInitialization,
        IOnApplicationShutdown, 
        IPreConfigureServices, 
        IPostConfigureServices

SUMMARY IAbpModuleDescriptor defined, nested, context service configuration

    public interface IAbpModuleDescriptor
    {
        Type Type { get; }
        Assembly Assembly { get; }
        IAbpModule Instance { get; }
        bool IsLoadedAsPlugIn { get; }
        IReadOnlyList<IAbpModuleDescriptor> Dependencies { get; }
    }
   public interface IAbpModule
    {
        void ConfigureServices(ServiceConfigurationContext context);
    }

依赖外部IServiceProvider的ApplicationExamples

  public interface IAbpApplicationWithExternalServiceProvider : IAbpApplication
    {
        void Initialize([NotNull] IServiceProvider serviceProvider);
    }

Focus abstract class that class abstract AbpApplicationBase : IAbpApplication

Appreciated ObjectAccessor, add an empty object access control, the value of the access unit will be assigned at the time of initialization, for example, among the above  IServiceProvider by  ObjectAccessor<T> wrapping up the object, whose value is NULL, but we can replace the back according to their needs specific knowledge Value Further, out, in covariance and

 // add AddOptions, AddLogging, AddLocalization and other infrastructure components. 
    services.AddCoreServices ();
     // add Abp core services, mainly related to modular system components. 
    services.AddCoreAbpServices ( the this , Options);

Modular

 // scanning module type, and a set of building blocks described objects. 
    var modules = GetDescriptors (Services, startupModuleType, plugInSources);
     // reordering module according to the dependency. 
    = modules SortByDependency (modules, startupModuleType);    
     // three lifecycle methods of calling module. 
    ConfigureServices (modules, services);

PreConfigureServices、ConfigureServices、PostConfigureServices

 Application Initialization Initialize

    public void Initialize()
        {
            ServiceScope = Services.BuildServiceProviderFromFactory().CreateScope();
            SetServiceProvider(ServiceScope.ServiceProvider);
            
            InitializeModules();
        }
BuildServiceProviderFromFactory, find IServiceProviderFactory, = "AbpAutofacServiceProviderFactory 
create ContainerBuilder, registration services to autofac
return IServiceProvider,
  services.Configure<AbpModuleLifecycleOptions>(options =>
            {
                options.Contributors.Add<OnPreApplicationInitializationModuleLifecycleContributor>();
                options.Contributors.Add<OnApplicationInitializationModuleLifecycleContributor>();
                options.Contributors.Add<OnPostApplicationInitializationModuleLifecycleContributor>();
                options.Contributors.Add<OnApplicationShutdownModuleLifecycleContributor>();
            });

  




 

Guess you like

Origin www.cnblogs.com/cloudsu/p/11766557.html