Develop blog project based on abp vNext and. NET Core - slim the project and make it run

Last article( https://www.cnblogs.com/meowv/p/12896177.html )The blog project has been created successfully, but abp references many components that are not used in the project by default.

This article will slim down the project and delete the components that are temporarily unavailable to us. Explain the relationship between the modules, write a Hello World and make it run successfully.

Slimming the project

Meowv.Blog.HttpApi.Hosting

Meowv.Blog.HttpApi.Hosting It is equivalent to a web project, but it mainly depends on Meowv.Blog.HttpApi Module to expose our API.

Delete Meowv.Blog.HttpApi.Hosting Files and folders generated by abp in the project, only Program.cs and Startup.cs Two classes.

In abp, each module should define a module class, derived from AbpModule, then add a module class MeowvBlogHttpApiHostingModule.cs

In the AbpModule class, you can perform a series of operations before and after the configuration service, application initialization, application initialization, application shutdown and module dependency. See details, https://docs.abp.io/en/abp/latest/Module-Development-Basics

For convenience, integrate Autofac directly here to replace the official dependency injection, https://docs.abp.io/zh-Hans/abp/latest/Autofac-Integration

So our module class looks like this.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Autofac;
using Volo.Abp.Modularity;

namespace Meowv.Blog.HttpApi.Hosting
{
    [DependsOn(
       typeof(AbpAspNetCoreMvcModule),
       typeof(AbpAutofacModule)
    )]
    public class MeowvBlogHttpApiHostingModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            base.ConfigureServices(context);
        }

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

            // Environment variables, development environment
            if (env.IsDevelopment())
            {
                // Generate exception page
                app.UseDeveloperExceptionPage();
            }

            // route
            app.UseRouting();

            // Route mapping
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

And then Startup.cs use, services.AddApplication <MeowvBlogHttpApiHostingModule>();

Continue to modify Program.cs Code, as follows:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System.Threading.Tasks;

namespace Meowv.Blog.HttpApi.Hosting
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            await Host.CreateDefaultBuilder(args)
                      .ConfigureWebHostDefaults(builder =>
                      {
                          builder.UseIISIntegration()
                                 .UseStartup<Startup>();
                      }).Build().RunAsync();
        }
    }
}

Then edit the project file, Meowv.Blog.HttpApi.Hosting.csproj Delete the useless configuration file information and components referenced by abp by default.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <Import Project="..\..\common.props" />

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Volo.Abp.Autofac" Version="2.7.0" />
    
    <ProjectReference Include="..\Meowv.Blog.HttpApi\Meowv.Blog.HttpApi.csproj" />
  </ItemGroup>

</Project>

Meowv.Blog.HttpApi

Meowv.Blog.HttpApi The main responsibility is to write Controller. All API s are written here. At the same time, it depends on Meowv.Blog.Application modular

First, delete the Models folder and the default Controller, modify the default module class name as: MeowvBlogHttpApiModule, and delete the useless dependencies

using Volo.Abp.Identity;
using Volo.Abp.Modularity;

namespace Meowv.Blog.HttpApi
{
    [DependsOn(
        typeof(AbpIdentityHttpApiModule)
    )]
    public class MeowvBlogHttpApiModule : AbpModule
    {

    }
}

Edit project file Meowv.Blog.HttpApi.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <Import Project="..\..\common.props" />

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Volo.Abp.Identity.HttpApi" Version="2.7.0" />
  </ItemGroup>

</Project>

Meowv.Blog.Application

Meowv.Blog.Application For our application service layer, write the service interface and corresponding implementation here

First of all, it's the module class, MeowvBlogApplicationModule. Here we should integrate cache and AutoMapper. Let's keep this one for a while. Let's run the project thin first and then talk about it.

using Volo.Abp.Identity;
using Volo.Abp.Modularity;

namespace Meowv.Blog.Application
{
    [DependsOn(
        typeof(AbpIdentityApplicationModule)
    )]
    public class MeowvBlogApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            
        }
    }
}

Create a new one MeowvBlogApplicationServiceBase.cs Inherited from ApplicationService, you can write an application service interface and implementation of Hello World first.

//IHelloWorldService.cs
namespace Meowv.Blog.Application.HelloWorld
{
    public interface IHelloWorldService
    {
        string HelloWorld();
    }
}
//HelloWorldService.cs
namespace Meowv.Blog.Application.HelloWorld.Impl
{
    public class HelloWorldService : ServiceBase, IHelloWorldService
    {
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

There are two points to note here: our services end with services, and each Service should inherit the base class ServiceBase we wrote

And then Meowv.Blog.HttpApi Add reference in layer: Meowv.Blog.Application At the same time, add the dependency module class, MeowvBlogApplicationModule

//MeowvBlogHttpApiModule.cs
using Meowv.Blog.Application;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;

namespace Meowv.Blog.HttpApi
{
    [DependsOn(
        typeof(AbpIdentityHttpApiModule),
        typeof(MeowvBlogApplicationModule)
    )]
    public class MeowvBlogHttpApiModule : AbpModule
    {

    }
}

Let it run

OK, it's almost the same after completing the above steps Meowv.Blog.HttpApi Add a Controller in, HelloWorldController.cs

using Meowv.Blog.Application.HelloWorld;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;

namespace Meowv.Blog.HttpApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HelloWorldController : AbpController
    {
        private readonly IHelloWorldService _helloWorldService;

        public HelloWorldController(IHelloWorldService helloWorldService)
        {
            _helloWorldService = helloWorldService;
        }

        [HttpGet]
        public string HelloWorld()
        {
            return _helloWorldService.HelloWorld();
        }
    }
}

ok, run it Meowv.Blog.HttpApi.Hosting Try it. It's unexpected that an error will be reported, because we are in the boot layer Meowv.Blog.HttpApi.Hosting Do not rely on the MeowvBlogHttpApiModule module. Try adding it

...
    [DependsOn(
       typeof(AbpAspNetCoreMvcModule),
       typeof(AbpAutofacModule),
       typeof(MeowvBlogHttpApiModule)
    )]
    public class MeowvBlogHttpApiHostingModule : AbpModule
    {
        ...
    }
...

Get it done. Hello World appears successfully. The goal is completed successfully.

Place a hierarchical list of current projects for reference

The above is the whole content of this chapter. For the time being, only three project layers are used to run successfully. Due to the time problem, there are still many areas that need to be improved without operation, but they will be gradually optimized when they are used later.

Open source address: https://github.com/Meowv/Blog/tree/blog_tutorial

Tags: SDK github

Posted on Mon, 18 May 2020 05:15:37 -0400 by zrosen88