using Autofac; using Autofac.Extensions.DependencyInjection; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.AutoTask; using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Redis; using log4net; using Service.Charger.Client; using SqlSugar; using SqlSugar.IOC; var builder = WebApplication.CreateBuilder(args); var log = LogManager.GetLogger(typeof(Program)); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(cb => { cb.Register(c => { var db = new SqlSugarScope(new ConnectionConfig { ConfigId = AppSettingsConstVars.ConfigId, ConnectionString = AppSettingsConstVars.DbSqlConnection, DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }, c => { c.Aop.OnLogExecuting = (sql, pars) => { string nativeSql = UtilMethods.GetNativeSql(sql, pars); log.Info(nativeSql); }; }); return db; }).As().InstancePerLifetimeScope(); cb.RegisterModule(new AutofacModuleRegister()); }); //redis var redisConnectionString = AppSettingsHelper.GetContent("Redis", "Connection"); var instanceName = AppSettingsHelper.GetContent("Redis", "InstanceName"); //默认数据库 var defaultDb = int.Parse(AppSettingsHelper.GetContent("Redis", "DefaultDB") ?? "0"); if (redisConnectionString != null && instanceName != null) builder.Services.AddSingleton(new RedisHelper(redisConnectionString, instanceName, defaultDb)); 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); }); //跨域 builder.Services.AddCors(options => { options.AddPolicy (name: "myCors", builde => { builde.WithOrigins("*", "*", "*") .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod(); } ); }); builder.Services.AddControllers().AddJsonOptions(configure => { configure.JsonSerializerOptions.Converters.Add(new DatetimeJsonConverter()); }); var app = builder.Build(); // Configure the HTTP request pipeline. app.UseSwagger(); app.UseSwaggerUI(); app.UseStaticFiles(); app.UseAuthorization(); app.UseCors("myCors"); app.MapControllers(); var list = AppSettingsHelper.GetContent("Kestrel", "Endpoints", "http", "Url"); foreach (var s in list.Split(";")) { log.Info($"use url={s}"); app.Urls.Add(s); } AppInfo.Container = app.Services.GetAutofacRoot(); ClientMgr.InitClient(); TaskInit.Init(); app.Run();