仓储-build
This commit is contained in:
187
src/WMS.Web.Repositories/DependencyInjection/AppBuilder.cs
Normal file
187
src/WMS.Web.Repositories/DependencyInjection/AppBuilder.cs
Normal file
@@ -0,0 +1,187 @@
|
||||
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 System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using WMS.Web.Core.Help;
|
||||
using WMS.Web.Domain.IService.Public;
|
||||
using WMS.Web.Domain.Mappers;
|
||||
using WMS.Web.Domain.Options;
|
||||
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();
|
||||
}
|
||||
/// <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"));
|
||||
}
|
||||
|
||||
/// <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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user