• 欢迎光临~

abp 在 .net core web 应用中接管请求

开发技术 开发技术 2022-05-19 次浏览

abp里面每个中间件都可以配置自己的依赖注入容器(重写 ConfigureServices ,然后通过上下文的 Services 属性获取ioc容器)和请求管道(重写 OnApplicationInitialization,然后通过上下文的GetApplicationBuilder 获取 applicationBuilder)

    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    public class App1Module : AbpModule
    {
        // 这里配置模块自己的容器
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            IServiceCollection services = context.Services;

            //services.AddTransient<IService1, Service1>();

            base.ConfigureServices(context);
        }

        // 这里配置模块自己的管道
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();

            app.UseRouting();

            app.UseEndpoints(endpoint => {
                endpoint.MapGet("/guid", async context => {
                    await context.Response.WriteAsync(Guid.NewGuid().ToString());
                });
            });

            base.OnApplicationInitialization(context);
        }
    }

项目结构图

abp 在 .net core web 应用中接管请求

 

 

1 创建 asp.net core mvc 项目,添加 Volo.Abp.AspNetCore.Mvc 包

项目Module类如下:

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

namespace AbpHelloworldWebMvc
{
    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    [DependsOn(typeof(App1Module))]
    public class AbpHelloworldWebMvcModule : AbpModule
    {
        // 在模块里面写自己的管道
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();
            var env = context.GetEnvironment();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            // 使用 abp 的扩展方法配置默认路由
            app.UseConfiguredEndpoints(options => {
                options.MapControllerRoute("default", "{controller=Home}/{action=privacy}/{id?}");
            });
        }
    }
}

Startup类如下:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AbpHelloworldWebMvc
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddApplication<AbpHelloworldWebMvcModule>();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.InitializeApplication();
        }
    }
}

2 创建项目 ClassLibrary1

模块类如下:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Modularity;

namespace ClassLibrary1
{
    [DependsOn(typeof(AbpAspNetCoreMvcModule))]
    public class App1Module : AbpModule
    {
        // 这里配置模块自己的容器
        public override void ConfigureServices(ServiceConfigurationContext context)
        {
            IServiceCollection services = context.Services;

            //services.AddTransient<IService1, Service1>();

            base.ConfigureServices(context);
        }

        // 这里配置模块自己的管道
        public override void OnApplicationInitialization(ApplicationInitializationContext context)
        {
            var app = context.GetApplicationBuilder();

            app.UseRouting();

            app.UseEndpoints(endpoint => {
                endpoint.MapGet("/guid", async context => {
                    await context.Response.WriteAsync(Guid.NewGuid().ToString());
                });
            });

            base.OnApplicationInitialization(context);
        }
    }
}

添加服务 IService1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace ClassLibrary1
{
    public interface IService1 
        : ITransientDependency
    {
        string Ping();
    }

    public class Service1 : IService1
    {
        public string Ping()
        {
            return "App1 Pong";
        }
    }
}

在项目 ClassLibrary1  添加控制器 App1Controller

using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;

namespace ClassLibrary1
{
    public class App1Controller : AbpController
    {
        IService1 _service1;

        public App1Controller(IService1 service1)
        {
            _service1 = service1;
        }

        public IActionResult Ping()
        {
            var resStr = _service1.Ping();

            return Content(resStr);
        }
    }
}

3 测试 

这时 web 项目为启动页

输入一下两个地址,访问 App1Module 里面配置的控制器和管道

http://localhost:5000/app1/ping

abp 在 .net core web 应用中接管请求


http://localhost:5000/guid

abp 在 .net core web 应用中接管请求

 

程序员灯塔
转载请注明原文链接:abp 在 .net core web 应用中接管请求
喜欢 (0)