303 lines
13 KiB
C#
303 lines
13 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.Hosting;
|
||
using Microsoft.OpenApi.Models;
|
||
using Newtonsoft.Json.Serialization;
|
||
using Quartz;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Text;
|
||
using WMS.Web.Core.Help;
|
||
using WMS.Web.Domain.IService;
|
||
using WMS.Web.Domain.IService.Public;
|
||
using WMS.Web.Domain.Mappers;
|
||
using WMS.Web.Domain.Options;
|
||
using WMS.Web.Domain.QuartzJob;
|
||
using WMS.Web.Domain.Services;
|
||
using WMS.Web.Domain.Services.Public;
|
||
|
||
namespace WMS.Web.Repositories.DependencyInjection
|
||
{
|
||
/// <summary>
|
||
/// App服务
|
||
/// </summary>
|
||
public class AppBuilder
|
||
{
|
||
/// <summary>
|
||
/// 服务集合
|
||
/// </summary>
|
||
public IServiceCollection Services { get; }
|
||
|
||
public IConfiguration Configuration { get; }
|
||
|
||
public IWebHostEnvironment WebHostEnvironment { get; }
|
||
|
||
/// <summary>
|
||
/// asf 服务
|
||
/// </summary>
|
||
/// <param name="services"></param>
|
||
public AppBuilder(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment webHostEnvironment)
|
||
{
|
||
Services = services;
|
||
Configuration = configuration;
|
||
WebHostEnvironment = webHostEnvironment;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 编译服务
|
||
/// </summary>
|
||
public void Build()
|
||
{
|
||
this.AddCors();
|
||
this.InitRedis();
|
||
this.AddSwagger();
|
||
this.AddOther();
|
||
this.AddConfigOptions();
|
||
this.AddServiceRepositories();
|
||
this.AddQuartzService();
|
||
}
|
||
/// <summary>
|
||
/// 其它功能注入:AutoMapper等其它
|
||
/// </summary>
|
||
private void AddOther()
|
||
{
|
||
Services.AddMvc().AddNewtonsoftJson(opt =>
|
||
{
|
||
//// 忽略循环引用
|
||
//opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
|
||
opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||
//// 不使用驼峰
|
||
//opt.SerializerSettings.ContractResolver = new DefaultContractResolver();
|
||
});
|
||
|
||
//AutoMapper映射关系
|
||
Services.AddAutoMapper(typeof(AppMapper).Assembly);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 跨域注入
|
||
/// </summary>
|
||
private void AddCors()
|
||
{
|
||
// 全局跨域注入
|
||
Services.AddCors(opt =>
|
||
{
|
||
string[] urls = Configuration.GetSection("AllowedCores").Value.Split(',');
|
||
opt.AddPolicy("AllowAllOrigin", builder =>
|
||
{
|
||
|
||
builder.AllowAnyMethod()
|
||
.AllowAnyHeader()
|
||
.AllowCredentials()
|
||
.WithOrigins(urls);
|
||
|
||
});
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化redis
|
||
/// </summary>
|
||
private void InitRedis()
|
||
{
|
||
var option = Configuration.GetSection("AppOptions").Get<AppOptions>();
|
||
var option_soa = Configuration.GetSection("SoaOptions").Get<SoaOptions>();
|
||
//初始化redis
|
||
RedisClient.redisClient.InitConnect(option.RedisConnectionString);
|
||
Services.AddHttpClient("ops_client", c =>
|
||
{
|
||
c.BaseAddress = new Uri(option_soa.Url);
|
||
});
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注册swagger
|
||
/// </summary>
|
||
private void AddSwagger()
|
||
{
|
||
if (WebHostEnvironment.IsDevelopment())
|
||
{
|
||
// 注册Swagger服务
|
||
Services.AddSwaggerGen(c =>
|
||
{
|
||
// 添加文档信息
|
||
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
|
||
{
|
||
Title = "WMS",
|
||
Version = "v1",
|
||
Description = "WMS-Api"
|
||
});
|
||
#region 读取xml信息
|
||
// 使用反射获取xml文件。并构造出文件的路径
|
||
//// 启用xml注释. 该方法第二个参数启用控制器的注释,默认为false.
|
||
foreach (var file in Directory.GetFiles(AppContext.BaseDirectory, "WMS.*.xml")) c.IncludeXmlComments(file, true);
|
||
#endregion
|
||
|
||
#region 启用swagger验证功能
|
||
//添加一个必须的全局安全信息,和AddSecurityDefinition方法指定的方案名称一致即可,CoreAPI。
|
||
var securit = new OpenApiSecurityRequirement()
|
||
{
|
||
{
|
||
new OpenApiSecurityScheme
|
||
{
|
||
Reference=new OpenApiReference { Type=ReferenceType.SecurityScheme,Id= "WMSAPI" }
|
||
},
|
||
new string[] { }
|
||
}
|
||
};
|
||
c.AddSecurityRequirement(securit);
|
||
c.AddSecurityDefinition("WMSAPI", new OpenApiSecurityScheme
|
||
{
|
||
Description = "JWT授权(数据将在请求头中进行传输) 在下方输入Bearer {token} 即可",
|
||
Name = "Authorization",//jwt默认的参数名称
|
||
In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
|
||
Type = SecuritySchemeType.ApiKey
|
||
});
|
||
#endregion
|
||
|
||
});
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 系统配置
|
||
/// </summary>
|
||
private void AddConfigOptions()
|
||
{
|
||
//系统配置注册
|
||
Services.AddOptions<AppOptions>();
|
||
Services.Configure<AppOptions>(Configuration.GetSection("AppOptions"));
|
||
Services.AddOptions<SoaOptions>();
|
||
Services.Configure<SoaOptions>(Configuration.GetSection("SoaOptions"));
|
||
Services.AddOptions<HttpOptions>();
|
||
Services.Configure<HttpOptions>(Configuration.GetSection("HttpOptions"));
|
||
Services.AddOptions<ErpOptions>();
|
||
Services.Configure<ErpOptions>(Configuration.GetSection("ErpOptions"));
|
||
Services.AddOptions<OpsOptions>();
|
||
Services.Configure<OpsOptions>(Configuration.GetSection("OpsOptions"));
|
||
Services.AddOptions<QiniuOptions>();
|
||
Services.Configure<QiniuOptions>(Configuration.GetSection("Qiniu"));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Quartz定时任务-可分布式集群
|
||
/// </summary>
|
||
private void AddQuartzService()
|
||
{
|
||
var options = Configuration.GetSection("QuartzJobOptions").Get<QuartzJobOptions>();
|
||
Services.AddTransient<InStockOrderQuartzJob>();//添加注入定时服务
|
||
Services.AddTransient<OutStockOrderQuartzJob>();//添加注入定时服务
|
||
Services.AddTransient<BoxQuartzJob>();//添加注入定时服务
|
||
//Services.AddTransient<OrderContractQuartzJob>();//添加注入定时服务
|
||
Services.AddQuartz(q =>
|
||
{
|
||
q.UsePersistentStore(x =>
|
||
{
|
||
//存储类型
|
||
x.Properties[options.JobStoreTypeKey] = options.JobStoreTypeValue;
|
||
//数据库驱动类型-这里是mysql
|
||
x.Properties[options.JobStoreDriverDelegateTypeKey] = options.JobStoreDriverDelegateTypeValue;
|
||
//表名前缀
|
||
x.Properties[options.JobStoreTablePrefixKey] = options.JobStoreTablePrefixValue;
|
||
//数据源名称
|
||
x.Properties[options.JobStoreDataSourceKey] = options.JobStoreDataSourceValue;
|
||
//连接字符串
|
||
x.Properties[options.JobStoreConnectionStringKey] = options.JobStoreConnectionStringValue;
|
||
//sqlserver版本
|
||
x.Properties[options.JobStoreProviderKey] = options.JobStoreProviderValue;
|
||
//是否启用集群:是
|
||
x.Properties[options.JobStoreClusteredKey] = options.JobStoreClusteredValue;
|
||
//集群节点都必须有一个唯一ID
|
||
x.Properties[options.JobStoreInstanceIdKey] = options.JobStoreInstanceIdValue;
|
||
x.UseProperties = true;
|
||
x.UseClustering();
|
||
x.UseJsonSerializer();
|
||
});
|
||
//用于注入
|
||
q.UseMicrosoftDependencyInjectionJobFactory();
|
||
|
||
#region 获取金蝶单据数据定时-配置
|
||
var jobKey = new JobKey("InStockOrderQuartzJob", options.QuartzJobValue);
|
||
q.AddJob<InStockOrderQuartzJob>(jobKey, j => j.WithDescription("InStockOrderQuartzJob"));
|
||
q.AddTrigger(t => t
|
||
.WithIdentity("InStockOrderQuartzJobTrigger")
|
||
.ForJob(jobKey)
|
||
.StartNow()
|
||
.WithCronSchedule(options.JobStartExpre)
|
||
//.WithCronSchedule(CronScheduleBuilder.DailyAtHourAndMinute(options.JobStartHour[5], options.JobStartMinute[5]))
|
||
.WithDescription("InStockOrderQuartzJobTriggerDecs"));
|
||
#endregion
|
||
|
||
#region 出库任务单
|
||
var jobKey_out = new JobKey("OutStockOrderQuartzJob", options.QuartzJobValue);
|
||
q.AddJob<OutStockOrderQuartzJob>(jobKey_out, j => j.WithDescription("OutStockOrderQuartzJob"));
|
||
q.AddTrigger(t => t
|
||
.WithIdentity("OutStockOrderQuartzJobTrigger")
|
||
.ForJob(jobKey_out)
|
||
.StartNow()
|
||
.WithCronSchedule(options.JobStartExpre)
|
||
//.WithCronSchedule(CronScheduleBuilder.DailyAtHourAndMinute(options.JobStartHour[5], options.JobStartMinute[5]))
|
||
.WithDescription("OutStockOrderQuartzJobTriggerDecs"));
|
||
#endregion
|
||
|
||
#region 箱信息老ops
|
||
var jobKey_box = new JobKey("BoxQuartzJob", options.QuartzJobValue);
|
||
q.AddJob<BoxQuartzJob>(jobKey_box, j => j.WithDescription("BoxQuartzJob"));
|
||
q.AddTrigger(t => t
|
||
.WithIdentity("BoxQuartzJobTrigger")
|
||
.ForJob(jobKey_box)
|
||
.StartNow()
|
||
.WithCronSchedule(options.JobStartExpre)
|
||
//.WithCronSchedule(CronScheduleBuilder.DailyAtHourAndMinute(options.JobStartHour[5], options.JobStartMinute[5]))
|
||
.WithDescription("BoxQuartzJobTriggerDecs"));
|
||
#endregion
|
||
});
|
||
//.net core核心托管-添加Quartz服务器
|
||
Services.AddQuartzServer(options =>
|
||
{
|
||
//关闭时,我们希望作业正常完成
|
||
options.WaitForJobsToComplete = false;
|
||
});
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 注入服务层
|
||
/// </summary>
|
||
/// <param name="builder"></param>
|
||
private void AddServiceRepositories()
|
||
{
|
||
Services.AddSingleton<RedisClientService>();
|
||
Services.AddTransient<HttpClientHelp>();
|
||
//注入服务
|
||
Services.AddTransient<ILoginService, LoginService>();
|
||
Services.AddTransient<IHttpClientService, HttpClientService>();
|
||
Services.AddTransient<ISingleDataService, SingleDataService>();
|
||
Services.AddTransient<IErpService, ErpService>();
|
||
Services.AddTransient<IBackRecordService, BackRecordService>();
|
||
Services.AddTransient<IOpsService, OpsService>();
|
||
|
||
Services.AddTransient<IErpBasicDataExtendService, ErpBasicDataExtendService>();
|
||
Services.AddTransient<IExportExcelService, ExportExcelService>();
|
||
Services.AddTransient<IQiniuUploadService, QiniuUploadService>();
|
||
|
||
|
||
Services.AddTransient<IBoxService, BoxService>();
|
||
Services.AddTransient<IChangeMoveBoxService, ChangeMoveBoxService>();
|
||
Services.AddTransient<ITakeStockService, TakeStockService>();
|
||
Services.AddTransient<IOutStockService, OutStockService>();
|
||
Services.AddTransient<IInStockService, InStockService>();
|
||
Services.AddTransient<IInStockTaskService, InStockTaskService>();
|
||
Services.AddTransient<IOutStockTaskService, OutStockTaskService>();
|
||
Services.AddTransient<ISerialNumberService, SerialNumberService>();
|
||
Services.AddTransient<IBoxInventoryService, BoxInventoryService>();
|
||
Services.AddTransient<IInventoryInOutDetailsService, InventoryInOutDetailsService>();
|
||
Services.AddTransient<IInventoryDetailsService, InventoryDetailsService>();
|
||
Services.AddTransient<IInStockTaskBoxService, InStockTaskBoxService>();
|
||
|
||
}
|
||
}
|
||
}
|