ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-05-19 08:01:52  阅读:142  来源: 互联网

标签:core web app System Microsoft abp context using public


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);
        }
    }

项目结构图

 

 

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


http://localhost:5000/guid

 

标签:core,web,app,System,Microsoft,abp,context,using,public
来源: https://www.cnblogs.com/tomorrow0/p/16287144.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有