You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.7 KiB

using System.Text;
7 months ago
using Autofac;
using Autofac.Extensions.DependencyInjection;
5 months ago
using Entity.Dto.Resp;
7 months ago
using HybirdFrameworkCore.Autofac;
using HybirdFrameworkCore.Configuration;
using HybirdFrameworkCore.Entity;
6 months ago
using HybirdFrameworkCore.Redis;
5 months ago
using Mapster;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Service.Cloud.Client;
using Service.Execute;
5 months ago
using Service.Execute.StaticTools;
using Service.Plc.Client;
7 months ago
using SqlSugar;
using SqlSugar.IOC;
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(cb =>
{
cb.Register(c =>
{
5 months ago
var db = new SqlSugarScope(new ConnectionConfig
7 months ago
{
ConfigId = AppSettingsConstVars.ConfigId,
5 months ago
ConnectionString = AppSettingsConstVars.DbSqlConnection, // 设置数据库连接字符串
7 months ago
DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer,
IsAutoCloseConnection = true,
InitKeyType = InitKeyType.Attribute // 如果使用实体类的属性进行主键标识,请设置为 InitKeyType.Attr
7 months ago
});
return db;
5 months ago
}).As<ISqlSugarClient>().SingleInstance();
7 months ago
7 months ago
cb.RegisterModule(new AutofacModuleRegister());
5 months ago
7 months ago
});
5 months ago
// 注册自定义映射
var mapper = new SysMenuMapper();
mapper.Register(TypeAdapterConfig.GlobalSettings);
//在 Autofac 容器中注册IHttpContextAccessor 服务
builder.Services.AddHttpContextAccessor();
5 months ago
//跨域
builder.Services.AddCors(options =>
{
options.AddPolicy
(name: "myCors",
builde =>
{
builde.WithOrigins("*", "*", "*")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
}
);
});
6 months ago
//redis
var section = builder.Configuration.GetSection("Redis");
//连接字符串
5 months ago
var redisConnectionString = section.GetSection("Connection").Value;
6 months ago
//实例名称
5 months ago
var instanceName = section.GetSection("InstanceName").Value;
6 months ago
//默认数据库
5 months ago
var _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "0");
6 months ago
if (redisConnectionString != null && instanceName != null)
builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, _defaultDB));
5 months ago
builder.Services.AddControllers().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
6 months ago
7 months ago
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Logging.AddLog4Net("log4net.config");
builder.Services.AddSwaggerGen(c =>
{
var basePath = Path.GetDirectoryName(AppContext.BaseDirectory);
//c.IncludeXmlComments(Path.Combine(basePath, Assembly.GetExecutingAssembly().GetName().Name+".xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "WebStarter.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "Entity.xml"), true);
c.IncludeXmlComments(Path.Combine(basePath, "HybirdFrameworkCore.xml"), true);
});
5 months ago
builder.Services.AddControllers().AddJsonOptions(configure =>
{
5 months ago
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
5 months ago
// 添加jwt验证
var securityKey = AppSettingsHelper.GetContent("TokenOptions", "SecurityKey");
var audience = AppSettingsHelper.GetContent("TokenOptions", "Audience");
var issuer = AppSettingsHelper.GetContent("TokenOptions", "Issuer");
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options => //配置鉴权逻辑
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(securityKey)),
ValidateLifetime = false
};
});
//Json 序列化
builder.Services.AddControllers().AddJsonOptions(configure =>
{
configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter());
});
5 months ago
7 months ago
var app = builder.Build();
7 months ago
7 months ago
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseStaticFiles();
7 months ago
}
app.UseAuthorization();
app.UseCors("myCors");
7 months ago
app.MapControllers();
7 months ago
AppInfo.Container = app.Services.GetAutofacRoot();
//云平台
CloudClientMgr.Init();
//PLC
PlcMgr.Init();
//启动换电流程
//StationSoftMgr.SwappingStateMachineStart();
//StationSoftMgr.StartTasks();
7 months ago
app.Run();