欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.NET Core使用APB vNext框架入门教程

程序员文章站 2022-10-12 14:51:19
目录快速入门案例.net core 控制台应用1. 安装 abp 框架核心依赖2. 新建 abp 应用的启动模块3. 新建服务,并注册到启动模块中4. 根据启动模块创建 abp应用,调用应用中注册的服...

快速入门案例

.net core 控制台应用

1. 安装 abp 框架核心依赖

install-package volo.abp.core -version 3.3.2

2. 新建 abp 应用的启动模块

helloabpmodule.cs

using volo.abp.modularity;

namespace helloabp
{
    /// <summary>
    /// 启动模块
    /// </summary>
    public class helloabpmodule : abpmodule
    {

    }
}

3. 新建服务,并注册到启动模块中

helloworldservice.cs

using system;
using volo.abp.dependencyinjection;

namespace helloabp
{
    /// <summary>
    /// todo: abp 注册服务方式一: 继承接口
    ///     isingletondependency、iscopeddependency、itransientdependency
    /// </summary>
    public class helloworldservice : itransientdependency
    {
        public void run()
        {
            console.writeline($"{nameof(helloabpmodule)}-{nameof(helloworldservice)}: hello world!");
        }
    }
}

4. 根据启动模块创建 abp应用,调用应用中注册的服务方法

program.cs

using system;
using microsoft.extensions.dependencyinjection;
using volo.abp;

namespace helloabp
{
    class program
    {
        static void main(string[] args)
        {
                // 根据启动模块创建 abp 应用
                var application = abpapplicationfactory.create<helloabpmodule>();
                // 初始化 abp 应用
                application.initialize();
                // 获取应用中注册的服务
                var service = application.serviceprovider.getservice<helloworldservice>();
                // 调用应用中的服务方法 
                service.run();

            console.readkey();
        }
    }
}

asp.net core web 应用程序

1. 安装 abp 框架核心依赖

install-package volo.abp.core -version 3.3.2
install-package volo.abp.aspnetcore.mvc -version 3.3.2

2.新建 abp 应用的启动模块

appmodule.cs

using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.http;
using microsoft.extensions.hosting;
using volo.abp;
using volo.abp.aspnetcore.mvc;
using volo.abp.modularity;

namespace hellowebabp
{
    /// <summary>
    /// 启动模块
    ///     todo: 1.在启动模块中配置 asp.net core web 程序的管道,就需要定义对 asp.net core mvc模块的依赖
    /// </summary>
    [dependson(typeof(abpaspnetcoremvcmodule))]
    public class appmodule : abpmodule
    {
        /// <summary>
        /// 应用初始化方法
        ///     todo: 2.重写 abp 应用的初始化方法,用来构建 asp.net core 应用程序的中间件管道
        /// </summary>
        /// <param name="context"></param>
        public override void onapplicationinitialization(applicationinitializationcontext context)
        {
            // base.onapplicationinitialization(context);

            var app = context.getapplicationbuilder();
            var env = context.getenvironment();

            if (env.isdevelopment())
            {
                app.usedeveloperexceptionpage();
            }

            app.userouting();

            // todo: 5.将 程序应用的端点配置 修改为 abp 应用的端点配置
            app.useconfiguredendpoints();
        }
    }
}

3. 注册 abp 启动模块,并初始化 abp 应用

startup.cs

using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.hosting;
using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;

namespace hellowebabp
{
    /// <summary>
    /// 程序启动类
    /// todo: 3. 在 startup 类中,完成对 abp 应用的初始化
    /// </summary>
    public class startup
    {
        // this method gets called by the runtime. use this method to add services to the container.
        // for more information on how to configure your application, visit https://go.microsoft.com/fwlink/?linkid=398940
        public void configureservices(iservicecollection services)
        {
            services.addapplication<appmodule>();
        }

        // this method gets called by the runtime. use this method to configure the http request pipeline.
        public void configure(iapplicationbuilder app, iwebhostenvironment env)
        {
            app.initializeapplication();
        }
    }
}

4. 新建控制器,测试 abp 应用运行状态

homecontroller.cs

using microsoft.aspnetcore.mvc;
using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using volo.abp.aspnetcore.mvc;

namespace hellowebabp.controllers
{
    /// <summary>
    /// 控制器
    ///     todo: 4. 继承 abp 框架中的基类控制器(提供了一些便捷的服务和方法)
    /// </summary>
    public class homecontroller : abpcontroller
    {
        public iactionresult index()
        {
            return content("hello world!");
        }
    }
}

各个击破案例

abp应用中的模块可以有很多个,但是启动模块只能有一个;

abp应用中的每个模块之间没有必然的联系;

abp应用中每个模块注册的服务,都注册到了abp应用的全局容器中;

abp应用中的模块也分为两种类型:应用程序模块(业务实现)和框架模块(技术实现);

abp应用中最顶层的模块是启动模块,最后被加载的也是启动模块。

在模块中注册自定义服务

helloabpmodule.cs

using system;
using system.collections.generic;
using system.text;
using microsoft.extensions.dependencyinjection;
using volo.abp;
using volo.abp.modularity;

namespace helloabp
{
    /// <summary>
    /// 启动模块
    /// </summary>
    public class helloabpmodule : abpmodule
    {
        // todo: 重写 abp 模块的服务配置方法,向模块中注册自定义的服务
        public override void configureservices(serviceconfigurationcontext context)
        {
            base.configureservices(context);

            // todo: abp 注册服务方式二: 模块注册
            context.services.addtransient<helloworldservice>();
        }
    }
}

小结

初始化abp模块

  • 1.注册abp基础设施与核心服务(模块系统相关)
  • 2.加载整个应用的所有模块,按照依赖性排序
  • 3.按顺序遍历所有模块,执行每一个模块的配置方法
  • 4.按顺序遍历所有模块,执行每一个模块的初始化方法

使用标签属性注册自定义服务

helloworldservice.cs

using system;
using system.collections.generic;
using system.text;
using microsoft.extensions.dependencyinjection;
using volo.abp.dependencyinjection;

namespace helloabp
{
    /// <summary>
    /// todo: abp 注册服务方式三: 特性标签
    ///     servicelifetime.singleton、servicelifetime.scoped、servicelifetime.transient
    /// </summary>
    [dependency(servicelifetime.transient)]
    public class helloworldservice
    {
        public void run()
        {
            console.writeline($"{nameof(helloabpmodule)}-{nameof(helloworldservice)}: hello world!");
        }
    }
}

abp 项目中使用 autofac

1. 安装 autofac 模块

install-package volo.abp.autofac -version 3.3.2

2. 在模块中创建对 autofac 模块的依赖

helloabpmodule.cs

using system;
using system.collections.generic;
using system.text;
using microsoft.extensions.dependencyinjection;
using volo.abp;
using volo.abp.autofac;
using volo.abp.modularity;

namespace helloabp
{
    /// <summary>
    /// 启动模块
    /// </summary>
    // todo: 使用 autofac 第三方依赖注入框架(提供了更多的高级特性)
    [dependson(typeof(abpautofacmodule))]
    public class helloabpmodule : abpmodule
    {
        public override void configureservices(serviceconfigurationcontext context)
        {
            base.configureservices(context);

            context.services.addtransient<helloworldservice>();
        }
    }
}

3. 在abp应用创建时集成 autofac

program.cs

using system;
using microsoft.extensions.dependencyinjection;
using volo.abp;

namespace helloabp
{
    class program
    {
        static void main(string[] args)
        {
            {
                // 根据启动模块创建 abp 应用
                var application = abpapplicationfactory.create<helloabpmodule>(options =>
                {
                    // 集成 autofac 
                    options.useautofac();
                });
                // 初始化 abp 应用
                application.initialize();
                // 获取应用中注册的服务
                var service = application.serviceprovider.getservice<helloworldservice>();
                // 调用应用中的服务方法 
                service.run();
            }

            console.writeline("hello world!");
            console.readkey();
        }
    }
}

完整案例代码

abpsample

到此这篇关于.net core使用apb vnext框架入门教程的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

上一篇: 虎父无犬子

下一篇: 知道得多