当前位置: 移动技术网 > IT编程>开发语言>.net > 【Net】ABP框架学习之正面硬钢

【Net】ABP框架学习之正面硬钢

2020年04月21日  | 移动技术网IT编程  | 我要评论

万秀猪王20131026,杨凯淳,南宫雪琪

前言

本文介绍另一种学习abp框架的方法,该方法为正面硬钢学习法。。。

我们不去官网下载模板,直接引用dll,直接使用。

webapi项目创建

首先创建一个webapi项目,结构如下。

然后nuget搜索abp,安装abp框架。(我这里安装的是5.1.0,因为最高版本安装不上)

在安装abp前先检查当前安装的microsoft.aspnet.webapi版本,因为abp5.1.0依赖的是webapi的5.2.7,如果webapi不是5.2.7,在nuget包管理—程序包管理器控制台中输入update-package microsoft.aspnet.webapi -version 5.2.7来升级一下。(get-package查看已安装包的信息)

然后修改global.asax,修改代码如下:

using abp.web;
using abpwebapi;
using system;
using system.web;
[assembly: preapplicationstartmethod(typeof(prestarter), "start")]
namespace abpwebapi
{
    public class webapiapplication : abp.web.abpwebapplication<sdudentapiservicemodule>
    {
        protected override void application_start(object sender, eventargs e)
        { 
            base.application_start(sender, e);
        } 
    }
    public static class prestarter
    {
        public static void start()
        {     //webapiapplication.abpbootstrapper.pluginsources.addtobuildmanager();
        }
    }
}

这里webapiapplication不再继承system.web.httpapplication,改为继承abp框架下的abp.web.abpwebapplication;因此原生框架提供的application_start不再需要,代码里重写了abp的application_start,这样我们就找到了application_start,可以在启动时做自己想做的事情了。

abpwebapplication是个泛型,要求指定默认启动模块的类,这里我们先写上sdudentapiservicemodule,下面会创建这个类。

在global中,还使用preapplicationstartmethod做了一些启动预处理,比如加载一些插件,当然也可以什么都不做。

如果要加载插件或者做一些其他操作,则需要再引入abp.web,因为一些配置的依赖库在这里,这里同样引用5.1.0版本。

因为使用了abp框架,所以我们不再需要微软提供的默认布局了,下面我们微软的默认布局文件夹删除;如下图:

现在我们新建一个类库,创建一个sdudentapi模块,用来编写可以被http访问的接口。

创建完类库后,我们需要在类库里添加一个自定义类,来标记,这个类库是webapi服务模块。

创建sdudentapiservicemodule类,并继承abpmodule。

很明显abpmodule在sdudentapi类库是未被引用的,所以我们要引用一下abp的框架。

因为这个模块是webapi,所以我们直接引用abp.web.api5.1.0就可以了。(由于abp.web.api依赖于abp,所以abp也会被同时引入)

现在我们编辑sdudentapiservicemodule类。

因为继承了abpmodule,所以我们可以override它preinitialize,initialize,postinitialize,shutdown;它们分别是模块初始化前,中,后和关闭。(只有被加载和关闭时调用这些方法,调用api方法时,这些不触发)

下面我们编写下sdudentapiservicemodule,代码如下:

[dependson(typeof(abp.webapi.abpwebapimodule))]
    public class sdudentapiservicemodule : abpmodule
    {
        public override void preinitialize()
        {
            configuration.modules.abpweb().antiforgery.isenabled = false;
            configuration.modules.abpwebcommon().sendallexceptionstoclients = true;  
        }
        public override void initialize()
        {
            //按照约定,abp自动注册所有 repositories, domain services, application services, mvc 控制器和web api控制器
            //abp按照约定注册程序集,下面代码将告诉abp要注册当前程序集。
            iocmanager.registerassemblybyconvention(assembly.getexecutingassembly());
            //动态apicontroller创建需要在将当前程序集注册进abp后,才可以调用
            //webapi访问路径默认前缀api/services,sdudent是我们追加的前缀,可以自定义,例如sdudent/task
            //外放成apicontroller的服务需要继承abp的iapplicationservice接口,需要准守命名约定,这样才能被搜索到(服务命名约定:服务名+appservice,例如searchsdudentappservice) 
            configuration.modules.abpwebapi().dynamicapicontrollerbuilder
            .forall<iapplicationservice>(assembly.getassembly(typeof(sdudentapiservicemodule)), "sdudent").build();  
        } 
        public override void postinitialize()
        { 
        } 
        public override void shutdown()
        {   
        }
    }

首先我们为sdudentapiservicemodule添加依赖[dependson(typeof(abp.webapi.abpwebapimodule))],这是因为,abp都是通过castle进行依赖控制反转实例化对象的,所以,在实例化sdudentapiservicemodule时,如果它依赖的类没有被装载,它就会报错,因为我们在写webapi,所以很明显,我们依赖abp.webapi.abpwebapimodule这个模块。

preinitialize:这里我们http请求的简单配置,还可以继续配置,abp配置很多。

initialize:这里将当前类装载进abp,同时动态创建了apicontroller。

postinitialize和shutdown暂时无操作。


现在我们创建服务(它们将被转换成apicontroller)。

创建接口isearchsdudentappservice,代码如下:

public interface isearchsdudentappservice :  iapplicationservice
    {
        [httpget]
        string getsdudent();
    }

注意接口方法需要加[httpget],不加的会被默认注册为post,测试时会出现无法访问的问题。

创建服务searchsdudentappservice,代码如下:

public class searchsdudentappservice: isearchsdudentappservice
    { 
        public string getsdudent()
        {
            return "i am a sdudent";
        }
    }

sdudentapiservicemodule编写完成,现在我们运行项目测试一下。

 如上图,访问成功。

url解析:这里我们访问的url是/api/services/sdudent/searchsdudent/getsdudent。

其中/api/services是默认前缀,sdudent/是我们自定义前缀,searchsdudent是controler名,它是根据服务名来的,服务名减去约定名(searchsdudentappservice-appservice),getsdudent是action名,就是服务里的方法名。


跨域配置

nuget搜索microsoft.aspnet.webapi.cors,安装与microsoft.aspnet.webapi相同版本号的cors。

sdudentapiservicemodule模块的preinitialize方法里追加配置。

var cors = new enablecorsattribute("*", "*", "*");
globalconfiguration.configuration.enablecors(cors);

filter配置

在sdudentapiservicemodule模块创建exceptionfilter类,代码如下:

public class exceptionfilter : iexceptionfilter, itransientdependency
{
    public bool allowmultiple => true; 
    public async task executeexceptionfilterasync(httpactionexecutedcontext actionexecutedcontext, cancellationtoken cancellationtoken)
    {
        await task.run(()=>
        {
​
            if (actionexecutedcontext == null)
            {
​
                return;
            }
            if (actionexecutedcontext.exception == null)
            {
                return;
            }
            //记录actionexecutedcontext.exception
        }); 
    } 
}

在sdudentapiservicemodule类的postinitialize里配置fliter。

public override void postinitialize()
{
    globalconfiguration.configuration.filters.add(new exceptionfilter());
} 

模块依赖

依照上文,在创建一个teacherapi模块,然后修改global的启动模块为teacherapiservicemodule,然后引入sdudentapi项目。

然后编写teacherapiservicemodule代码如下:

[dependson(typeof(abp.webapi.abpwebapimodule), typeof(sdudentapiservicemodule))]
public class teacherapiservicemodule : abpmodule
{
    private readonly sdudentapiservicemodule _sdudentapiservicemodule;
    public teacherapiservicemodule(sdudentapiservicemodule sdudentapiservicemodule)
    {
        _sdudentapiservicemodule = sdudentapiservicemodule;
    }
    public override void preinitialize()
    {
        configuration.modules.abpweb().antiforgery.isenabled = false;
        configuration.modules.abpwebcommon().sendallexceptionstoclients = true; 
    }
    public override void initialize()
    { 
        iocmanager.registerassemblybyconvention(assembly.getexecutingassembly()); 
        configuration.modules.abpwebapi().dynamicapicontrollerbuilder
        .forall<iapplicationservice>(assembly.getassembly(typeof(teacherapiservicemodule)), "sdudent").build(); 
    }
    public override void postinitialize()
    {
        var ret =_sdudentapiservicemodule.getsdudent();
        console.writeline(ret);
    } 
    public override void shutdown()
    {   
    }
}

如上代码所示,我们在dependson上追加sdudentapiservicemodule依赖,然后在teacherapiservicemodule的构造函数里,使用sdudentapiservicemodule类型参数,然后运行时参数就会被实例化,并注入进来。

现在我们访问teacherapi的访问,把teacherapiservicemodule模块调用起来,看下模块的postinitialize里,是否成功调用了sdudentapiservicemodule模块的方法。

如上图,依赖调用成功。

swagger配置

nuget搜索swashbuckle.core。

模块下添加函数

private void configureswaggerui()
{
    configuration.modules.abpwebapi().httpconfiguration
        .enableswagger(c =>
        {
            c.singleapiversion("v1", "文档");
            c.resolveconflictingactions(apidescriptions => apidescriptions.first());
        })
        .enableswaggerui();
}

然后在initialize()里调用该函数,配置完成。

然后运行项目,输入swagger/ui/index,如下图:

----------------------------------------------------------------------------------------------------

代码已经传到github上了,欢迎大家下载。

github地址: https://github.com/kiba518/apbwebapi

----------------------------------------------------------------------------------------------------

注:此文章为原创,任何形式的转载都请联系作者获得授权并注明出处!
若您觉得这篇文章还不错,请点击下方的推荐】,非常感谢!

 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网