first commit
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
using Autofac;
|
||||
using FluentValidation;
|
||||
using FluentValidation.AspNetCore;
|
||||
using JSMachine.WMS.Domain.Repository;
|
||||
using JSMachine.WMS.Infrastructure;
|
||||
using JSMachine.WMS.Infrastructure.Helper;
|
||||
using JSMachine.WMS.Job;
|
||||
using JSMachine.WMS.PLC.ModbusImpl.Elevator.Exstension;
|
||||
using JSMachine.WMS.WebHost.Exstention;
|
||||
using JSMachine.WMS.WebHost.Filters;
|
||||
using JSMachine.WMS.WebHost.Validators;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using SqlSugar;
|
||||
using System.Reflection;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace JSMachine.WMS.WebHost
|
||||
{
|
||||
/// <summary>
|
||||
/// WebHost 的服务注册和 HTTP 请求管道配置入口。
|
||||
/// </summary>
|
||||
public class Startup
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建启动配置对象。
|
||||
/// </summary>
|
||||
/// <param name="configuration">应用配置,包含数据库、ERP、RCS 和设备配置。</param>
|
||||
public Startup(IConfiguration configuration)
|
||||
{
|
||||
Configuration = configuration;
|
||||
}
|
||||
|
||||
public IConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 注册控制器、参数验证、跨域、对象映射、缓存、数据库及应用服务所需的依赖。
|
||||
/// </summary>
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllers(op =>
|
||||
{
|
||||
//op.Filters.Add<RateLimitFilter>();
|
||||
//op.Filters.Add<CustomActionFilter>();
|
||||
op.Filters.Add<ExceptionFilter>();
|
||||
})
|
||||
.AddNewtonsoftJson(jsonOp =>
|
||||
{
|
||||
jsonOp.SerializerSettings.ContractResolver = null;
|
||||
jsonOp.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
|
||||
jsonOp.SerializerSettings.Converters.Add(new StringEnumConverter());
|
||||
});
|
||||
|
||||
|
||||
//services.AddAuthentication("Bearer")
|
||||
// .AddJwtBearer("Bearer", options =>
|
||||
// {
|
||||
// options.Authority = Configuration["AuthorityUrl"];
|
||||
// options.TokenValidationParameters = new TokenValidationParameters { ValidateAudience = false };
|
||||
// options.RequireHttpsMetadata = false;
|
||||
// });
|
||||
//services.AddAuthorization(option =>
|
||||
//{
|
||||
// option.AddPolicy("DefaultPolicy", builder =>
|
||||
// {
|
||||
// builder.RequireAuthenticatedUser();
|
||||
// builder.RequireClaim("scope", "PostOrderApi");
|
||||
// });
|
||||
//});
|
||||
|
||||
services.AddFluentValidationAutoValidation(p => p.DisableDataAnnotationsValidation = true);
|
||||
services.AddValidatorsFromAssemblyContaining<BarCodeUploadParamValidator>();
|
||||
|
||||
services.AddCors(option => option.AddPolicy("any", build =>
|
||||
{
|
||||
build
|
||||
.AllowAnyHeader()
|
||||
.AllowAnyOrigin()
|
||||
.AllowAnyMethod();
|
||||
}));
|
||||
|
||||
services.AddAutoMapper();
|
||||
|
||||
services.AddSingleton<IMemoryCache, MemoryCache>();
|
||||
|
||||
services.AddSingleton(s =>
|
||||
{
|
||||
SqlSugarScope sqlSugarScope = new(new BccDbConnectionConfig().ConnectionConfig);
|
||||
|
||||
sqlSugarScope.Aop.OnLogExecuting = async (sql, pars) =>
|
||||
{
|
||||
|
||||
//await File.WriteAllTextAsync(@"C:\Users\admin\Desktop\123.txx", sql);
|
||||
};
|
||||
|
||||
sqlSugarScope.Ado.CommandTimeOut = 5;
|
||||
|
||||
sqlSugarScope.Aop.OnError = error =>
|
||||
{
|
||||
LogHelper.Error($"操作数据库出错 {error.Message} {error.StackTrace}");
|
||||
};
|
||||
|
||||
return sqlSugarScope;
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 配置 HTTP 请求处理中间件。中间件顺序决定跨域、路由、认证授权和控制器执行时机,
|
||||
/// 末尾启动后台任务及电梯 PLC 通信功能。
|
||||
/// </summary>
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
{
|
||||
app.UseCors("any");
|
||||
if (env.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseAutoMapperConfig();
|
||||
app.UseGlobalServiceProvider();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
});
|
||||
|
||||
app.UseAllJobs();
|
||||
|
||||
app.UseElevatorPlc();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用 Autofac 扫描领域、应用、业务和 RPC 程序集,注册约定命名的服务实现。
|
||||
/// </summary>
|
||||
/// <param name="builder">Autofac 容器构建器。</param>
|
||||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
{
|
||||
builder
|
||||
.RegisterAssemblyTypes([
|
||||
Assembly.LoadFrom($"{AppDomain.CurrentDomain.BaseDirectory}JSMachine.WMS.Domain.dll"),
|
||||
Assembly.LoadFrom($"{AppDomain.CurrentDomain.BaseDirectory}JSMachine.WMS.App.dll"),
|
||||
Assembly.LoadFrom($"{AppDomain.CurrentDomain.BaseDirectory}JSMachine.WMS.Business.dll"),
|
||||
Assembly.LoadFrom($"{AppDomain.CurrentDomain.BaseDirectory}JSMachine.WMS.RPC.dll")
|
||||
])
|
||||
.Where(x => x.Name.EndsWith("Repository", StringComparison.OrdinalIgnoreCase) ||
|
||||
x.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase)||
|
||||
x.Name.EndsWith("Strategy", StringComparison.OrdinalIgnoreCase)||
|
||||
x.Name.EndsWith("Handler", StringComparison.OrdinalIgnoreCase))
|
||||
.AsImplementedInterfaces()
|
||||
.AsSelf()
|
||||
.SingleInstance();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user