欢迎访问 生活随笔!

凯发ag旗舰厅登录网址下载

当前位置: 凯发ag旗舰厅登录网址下载 > 编程语言 > >内容正文

asp.net

基于 abp vnext 和 .net core 开发博客项目 -凯发ag旗舰厅登录网址下载

发布时间:2025/1/21 18 豆豆
凯发ag旗舰厅登录网址下载 收集整理的这篇文章主要介绍了 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

基于 abp vnext 和 .net core 开发博客项目 - 给项目瘦身,让它跑起来
转载于:https://github.com/meowv/blog.git

本篇文章将给项目进行瘦身,删掉对我们来说暂时用不到的组件。讲解各个模块之间的关系,写一个hello world,让其成功运行起来。

给项目瘦身

.httpapi.hosting

meowv.blog.httpapi.hosting相当于一个web项目,但这里主要依赖于meowv.blog.httpapi模块,用来暴露我们的api的。

删掉meowv.blog.httpapi.hosting项目中abp自己生成的文件和文件夹,只留下program.cs和startup.cs两个类。

在abp中,每个模块都应该定义一个模块类,派生自abpmodule,那么就添加一个模块类meowvbloghttpapihostingmodule.cs

abpmodule类中可以做 配置服务前和后的操作,应用程序初始化,应用程序初始化前和后,应用程序关闭和模块依赖等一系列操作,详看,https://docs.abp.io/en/abp/latest/module-development-basics

为了方便,在这里直接集成autofac,来替换官方依赖注入,详看,https://docs.abp.io/zh-hans/abp/latest/autofac-integration

于是我们的模块类就变成下面这个样子。

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();// 环境变量,开发环境if (env.isdevelopment()){// 生成异常页面app.usedeveloperexceptionpage();}// 路由app.userouting();// 路由映射app.useendpoints(endpoints =>{endpoints.mapcontrollers();});} }

}

然后在startup.cs使用,services.addapplication();

继续修改program.cs代码,如下:

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();
}).useautofac().build().runasync();
}
}
}

然后编辑一下项目文件,meowv.blog.httpapi.hosting.csproj,删掉无用的配置文件信息和abp默认引用的组件。

netcoreapp3.1

.httpapi

meowv.blog.httpapi职责主要是编写controller,所有的api都写在这里,同时它要依赖于meowv.blog.application模块

先删掉models文件夹和默认的controller,修改默认模块类名称为:meowvbloghttpapimodule,并删掉无用的依赖项

using volo.abp.identity;
using volo.abp.modularity;

namespace meowv.blog.httpapi
{
[dependson(
typeof(abpidentityhttpapimodule)
)]
public class meowvbloghttpapimodule : abpmodule
{

}

}

编辑项目文件meowv.blog.httpapi.csproj

netcoreapp3.1

.application

meowv.blog.application为我们的应用服务层,在这里编写服务的接口以及对应的实现

首先还是模块类,meowvblogapplicationmodule,在这里我们应该集成缓存和automapper,这个先留着,先把项目瘦身跑起来后面在说吧。

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)
{

} }

}
新建一个meowvblogapplicationservicebase.cs继承自applicationservice,然后就可以先写一个hello world的应用服务接口和实现。

//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”;
}
}
}

这里有两点需要注意,凯发ag旗舰厅登录网址下载的服务都以service结尾,每个服务都应该继承我们编写的的基类servicebase

然后在meowv.blog.httpapi层中添引用:meowv.blog.application,同时添加依赖模块类,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
{

}

}

让它跑起来

好了,完成以上步骤,基本上差不多了,在meowv.blog.httpapi中新增一个 controller,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,运行一下meowv.blog.httpapi.hosting试试看,不出意料应该会报错的,因为我们在启动层meowv.blog.httpapi.hosting没有依赖meowvbloghttpapimodule模块,添加上试试


[dependson(
typeof(abpaspnetcoremvcmodule),
typeof(abpautofacmodule),
typeof(meowvbloghttpapimodule)
)]
public class meowvbloghttpapihostingmodule : abpmodule
{

}

搞定,成功出现hello world,目标圆满完成。

图片

放一张此时项目的层级目录,以供参考

图片

以上就是本章全部内容,暂时只用到了其中三个项目层便成功运行,由于时间问题,其中还有许多需要完善的地方没有操作,但后续用到的时候会逐步优化掉。

开源地址:https://github.com/meowv/blog/tree/blog_tutorial

总结

以上是凯发ag旗舰厅登录网址下载为你收集整理的的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。

网站地图