【愚公系列】2022年02月 .NET架构班 003-ABP vNext在.NET Core WEB项目使用

举报
愚公搬代码 发表于 2022/02/16 15:26:43 2022/02/16
【摘要】 前言配置环境vs2022NET 6window11 一、ABP vNext在.NET Core WEB项目使用 1.安装依赖包Volo.Abp.AspNetCore.MvcVolo.Abp.Autofac 二、模块类 1.创建模块类WebModule/// <summary>/// Web 模块/// </summary>[DependsOn(typeof(AbpAspNetCoreMvc...

前言

配置环境

  • vs2022
  • NET 6
  • window11

一、ABP vNext在.NET Core WEB项目使用

1.安装依赖包

Volo.Abp.AspNetCore.Mvc
Volo.Abp.Autofac

在这里插入图片描述

二、模块类

1.创建模块类

WebModule

/// <summary>
/// Web 模块
/// </summary>
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
public class WebModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        // Add services to the container.

        context.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        context.Services.AddEndpointsApiExplorer();
        context.Services.AddSwaggerGen();
    }

    public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
    {
        base.OnPreApplicationInitialization(context);
    }

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();//使用swagger
            app.UseSwaggerUI();//使用swaggerUI
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseConfiguredEndpoints();
    }

    public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
    {
        base.OnPostApplicationInitialization(context);
    }

    public override void OnApplicationShutdown(ApplicationShutdownContext context)
    {
        base.OnApplicationShutdown(context);
    }
}

2.注册模块类

using ABPWEBAPI;

var builder = WebApplication.CreateBuilder(args);

builder.Services.ReplaceConfiguration(builder.Configuration);

builder.Services.AddApplication<WebModule>();

var app = builder.Build();

app.InitializeApplication();

//app.MapControllers();

app.Run();

运行成功后会出现swagger如下:
在这里插入图片描述

三、服务类

1.创建服务类

WebService服务类

using Volo.Abp.DependencyInjection;

namespace ABPWEBAPI.Services
{
    [Dependency(ServiceLifetime.Transient, ReplaceServices = true)]
    public class WebService
    {
        public void Show()
        {
            System.Console.WriteLine("WebService 执行");
        }
    }
}

四、Autofac的配置

1.安装依赖包

Volo.Abp.Autofac

2.服务属性配置

Program配置

using ABPWEBAPI;

var builder = WebApplication.CreateBuilder(args);

builder.Host.UseAutofac();

builder.Services.ReplaceConfiguration(builder.Configuration);

builder.Services.AddApplication<WebModule>();

var app = builder.Build();

app.InitializeApplication();

//app.MapControllers();

app.Run();

WebModule模块类配置

/// <summary>
/// Web 模块
/// </summary>
[DependsOn(typeof(AbpAspNetCoreMvcModule))]
[DependsOn(typeof(AbpAutofacModule))] // 在模块上添加依赖AbpAutofacModule
public class WebModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        // Add services to the container.

        context.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
        context.Services.AddEndpointsApiExplorer();
        context.Services.AddSwaggerGen();
    }

    public override void OnPreApplicationInitialization(ApplicationInitializationContext context)
    {
        base.OnPreApplicationInitialization(context);
    }

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var env = context.GetEnvironment();

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwagger();//使用swagger
            app.UseSwaggerUI();//使用swaggerUI
        }
        else
        {
            app.UseExceptionHandler("/Error");
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseRouting();
        app.UseConfiguredEndpoints();
    }

    public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
    {
        base.OnPostApplicationInitialization(context);
    }

    public override void OnApplicationShutdown(ApplicationShutdownContext context)
    {
        base.OnApplicationShutdown(context);
    }
}

3.使用属性注入

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
    private static readonly string[] Summaries = new[]
    {
    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

    private readonly ILogger<WeatherForecastController> _logger;
    public WebService _webService { set; get; }

    public WeatherForecastController(ILogger<WeatherForecastController> logger)
    {
        _logger = logger;
    }

    [HttpGet(Name = "GetWeatherForecast")]
    public IEnumerable<WeatherForecast> Get()
    {
        #region 1、Web服务Service调用
        {
            _webService.Show();
        }
        #endregion
        return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        {
            Date = DateTime.Now.AddDays(index),
            TemperatureC = Random.Shared.Next(-20, 55),
            Summary = Summaries[Random.Shared.Next(Summaries.Length)]
        })
        .ToArray();
    }
}

在这里插入图片描述
其他使用方式参考上节内容

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。