温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET Core Zero模块系统的示例分析

发布时间:2022-02-15 13:39:03 来源:亿速云 阅读:121 作者:小新 栏目:开发技术

这篇文章主要为大家展示了“ASP.NET Core Zero模块系统的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“ASP.NET Core Zero模块系统的示例分析”这篇文章吧。

简介

在ABP中, 模板的定义就是一个类, 只需要继承 AbpModule, 该类可以通过nuget包搜索 ABP 安装。

下面演示在应用程序或类库中, 定义一个模块:

 public class ApplicationModule : AbpModule
    {
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(typeof(ApplicationModule).GetAssembly());
        }
    }

说明: 关于IocManager.RegisterAssemblyByConvention的作用, 则是将当前程序集模块注册到容器当中, 作为一个模块, 常见的是暴露模块对应的服务,
而其中所有的服务, 都是按照声明周期而声明, 例如: ITransientDependency ,ISingletonDependency。

下面展示了IocManager.RegisterAssemblyByConvention 执行的部分细节:

public void RegisterAssembly(IConventionalRegistrationContext context)
{
            //Transient
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ITransientDependency>()
                    .If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
                    .WithService.Self()
                    .WithService.DefaultInterfaces()
                    .LifestyleTransient()
                );

            //Singleton
            context.IocManager.IocContainer.Register(
                Classes.FromAssembly(context.Assembly)
                    .IncludeNonPublicTypes()
                    .BasedOn<ISingletonDependency>()
                    .If(type => !type.GetTypeInfo().IsGenericTypeDefinition)
                    .WithService.Self()
                    .WithService.DefaultInterfaces()
                    .LifestyleSingleton()
                );

            //...
}

常见的方法

在AbpModule中, 定义了几组方法, 分别在应用程序模块加载的前后进行, 如下:

        public virtual void Initialize();
        public virtual void PostInitialize();
        public virtual void PreInitialize();
        public virtual void Shutdown();
  • Initialize : 通常, 这里用于注册程序集依赖选项

  • PostInitialize : 初始化最后调用

  • PreInitialize : 初始化之前调用

  • Shutdown : 当应用程序关闭时调用

模块依赖

通常来讲, 一个模块往往依赖与一个或者多个模块, 这里, 也涉及到了模块的加载生命周期。
假设: 模块A依赖于模块B, 那么意味着模块B会先于模块A初始化。

关于模块之间的依赖, 则可以通过特性的方式 DependsOn 为模块显示声明, 如下所示:

[DependsOn(typeof(BModule))]
public class AModule : AbpModule
{
    public override void Initialize()
    {
        //...
    }
}

模块加载

任何模块都依赖于启动模块进行加载, 这很常见, 例如机箱中的各个模块: 内存、硬盘、显卡、电源。 都需要通电的过程, 让他们进行整个启动过程。
Abp 则依赖于 AbpBootstrapper 来进行调用初始化, 可以通过 Initialize 方法加载。

 public static class ApplicationBootstrapper
    {
        public static AbpBootstrapper AbpBootstrapper { get; private set; }

       public static void Init(){
         //...
         AbpBootstrapper.Initialize();
       }
    }

同样, 模块也可以读取指定文件夹路径的方式进行加载模块, 如下所示:

services.AddAbp<MyStartupModule>(options =>
{
    options.PlugInSources.Add(new FolderPlugInSource(@"C:\MyPlugIns"));
});

or

services.AddAbp<MyStartupModule>(options =>
{
    options.PlugInSources.AddFolder(@"C:\MyPlugIns");
});

以上是“ASP.NET Core Zero模块系统的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI