using Autofac; using Autofac.Extensions.DependencyInjection; using HybirdFrameworkCore.Autofac; using HybirdFrameworkCore.Configuration; using HybirdFrameworkCore.Entity; using HybirdFrameworkCore.Redis; using Service.Charger.Client; using SqlSugar; using SqlSugar.IOC; var builder = WebApplication.CreateBuilder(args); 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 // ���ʹ��ʵ��������Խ���������ʶ��������Ϊ InitKeyType.Attribute }); return db; }).As().InstancePerLifetimeScope(); cb.RegisterModule(new AutofacModuleRegister()); }); //redis var section = builder.Configuration.GetSection("Redis"); //连接字符串 var redisConnectionString = section.GetSection("Connection").Value; //实例名称 var instanceName = section.GetSection("InstanceName").Value; //默认数据库 var _defaultDB = int.Parse(section.GetSection("DefaultDB").Value ?? "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. if (app.Environment.IsDevelopment()) { 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(";")) { app.Urls.Add(s); } AppInfo.Container = app.Services.GetAutofacRoot(); ClientMgr.InitClient(); app.Run();