尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

命令行一样能用ABP

命令行一样能用ABP 文章目录新建项目一次性执行注释host run获取IOC容器添加必要的Module优雅地解析命令行新建项目参照官方文档一次性执行官方生成的demo是一个host服务会一直在跑如果我们想做成执行一次就退出需要如下改动注释host run//await host.RunAsync();如果你不要host服务也可以注释掉视情况而定//builder.Services.AddHostedServiceExcel2DBHostedService();获取IOC容器awaithost.InitializeAsync();//await host.RunAsync();variochost.Services.GetRequiredServiceIAbpLazyServiceProvider();再通过IOC容器获取所需服务varserviceioc.LazyGetRequiredServiceYourService();添加必要的Module官方模板只添加了最基本的Volo.Abp.Autofac此时这还不算一个真正的ABP应用。要想使用ABP的很多基础功能你需要添加NuGet\Install-PackageVolo.Abp.Ddd.Application[DependsOn(typeof(AbpAutofacModule),typeof(AbpDddApplicationModule)//加这行)]我们通过启动日志就能看到很多abp基础模块此时才加进来优雅地解析命令行微软提供了专门的工具System.CommandLine来解析用户传入的参数这里做一个简单的demovarrootCommandnewRootCommand(Root Command);varfileOptionnewOptionFileInfo(--file,[-f])//定义缩写{DescriptionExcel full path.};varexnewCommand(excel,decode excel data and store in db)//discription{fileOption};ex.SetAction(result{varfileInforesult.GetValue(fileOption);//这里直接拿到泛型类if(fileInfo!null){varmanagerioc.LazyGetRequiredServiceExcel2DbManager();manager.InsertSapHighbayInventory(fileInfo);}});rootCommand.Add(ex);varparserootCommand.Parse(args);returnparse.Invoke();在命令行执行YourApp.exe excel-h会输出选项:-f,--file file Excel full path.-?,-h,--help Show help and usage information一切是那么的优雅
返回列表