IServer负责监听http请求,转发到HttpApplication,创建httpcontext,处理请求,释放资源
因此,Application包含有模块注册,服务的注册IServiceCollection,依赖注入IServiceProvider,通过startup进行注册和组成中间件管道,关闭和释放操作。
理解Startup和abp模块之前的关系
1、services.AddApplication<DemoAppModule>() =>DemoAppModule是abp模块,它本身也依赖其它模块,形成依赖树
2、使用autofac在哪里替换,拓展方法
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模块化
public abstract class AbpModule : IAbpModule, IOnPreApplicationInitialization, IOnApplicationInitialization, IOnPostApplicationInitialization, IOnApplicationShutdown, IPreConfigureServices, IPostConfigureServices
IAbpModuleDescriptor定义,嵌套,服务配置的上下文的内容
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的Application
实例
public interface IAbpApplicationWithExternalServiceProvider : IAbpApplication { void Initialize([NotNull] IServiceProvider serviceProvider); }
重点的在于 抽象类abstract class AbpApplicationBase : IAbpApplication
理解ObjectAccessor,添加一个空的对象访问器,该访问器的值会在初始化的时候被赋值,例如在上文当中的 IServiceProvider
通过 ObjectAccessor<T>
对象包裹起来,其值是 NULL,但是在后面我们可以根据自己的需要替换其具体的 Value 另外知识点,out,in 协变和逆变
// 添加AddOptions,AddLogging,AddLocalization等基础设施组件。 services.AddCoreServices(); // 添加核心的 Abp 服务,主要是模块系统相关组件。 services.AddCoreAbpServices(this, options);
模块化
// 扫描模块类型,并构建模块描述对象集合。 var modules = GetDescriptors(services, startupModuleType, plugInSources); // 按照模块的依赖性重新排序。 modules = SortByDependency(modules, startupModuleType); // 调用模块的三个生命周期方法。 ConfigureServices(modules, services);
PreConfigureServices、ConfigureServices、PostConfigureServices
Application初始化 Initialize
public void Initialize() { ServiceScope = Services.BuildServiceProviderFromFactory().CreateScope(); SetServiceProvider(ServiceScope.ServiceProvider); InitializeModules(); }
BuildServiceProviderFromFactory,查找到IServiceProviderFactory,=》AbpAutofacServiceProviderFactory
创建ContainerBuilder,注册服务到autofac
返回IServiceProvider,
services.Configure<AbpModuleLifecycleOptions>(options => { options.Contributors.Add<OnPreApplicationInitializationModuleLifecycleContributor>(); options.Contributors.Add<OnApplicationInitializationModuleLifecycleContributor>(); options.Contributors.Add<OnPostApplicationInitializationModuleLifecycleContributor>(); options.Contributors.Add<OnApplicationShutdownModuleLifecycleContributor>(); });