using Autofac; using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System.Reflection; using Txgy.RBS.Framework.RedisHelper.Service; namespace Txgy.RBS.Server.WebApi.Register { public static class HostBuilderExtend { public static void Register(this WebApplicationBuilder applicationBuilder) { //替换容器Autofac applicationBuilder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); applicationBuilder.Host.ConfigureContainer(ConfigurationBinder => { //ConfigurationBinder.RegisterType().As(); #region 通过接口和实现类所在程序集注册 Assembly interfaceAssembly = Assembly.Load("Txgy.RBS.Services"); Assembly serviceAssembly = Assembly.Load("Txgy.RBS.IServices"); ConfigurationBinder.RegisterAssemblyTypes(interfaceAssembly, serviceAssembly).AsImplementedInterfaces(); #endregion #region 注册每个控制器和抽象之间的关系 var controllerBaseType = typeof(ControllerBase); ConfigurationBinder.RegisterAssemblyTypes(typeof(Program).Assembly) .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType); #endregion #region 注册SqlSugar ConfigurationBinder.Register(context => { SqlSugarClient client = new SqlSugarClient(new ConnectionConfig() { ConnectionString = applicationBuilder.Configuration.GetConnectionString("ConnectionSqlite"), // "Data Source=DESKTOP-T2D6ILD;Initial Catalog=LiveBackgroundManagementNew;Persist Security Info=True;User ID=sa;Password=sa123", DbType = DbType.Sqlite, InitKeyType = InitKeyType.Attribute, //SlaveConnectionConfigs = new List { // new SlaveConnectionConfig(){ // ConnectionString="Data Source=DESKTOP-T2D6ILD;Initial Catalog=LiveBackgroundManagementNew;Persist Security Info=True;User ID=sa;Password=sa123", // HitRate=10 // } // } }); //支持sql语句的输出,方便排除错误 client.Aop.OnLogExecuting = (sql, par) => { Console.WriteLine("\r\n"); Console.WriteLine($"Sql语句:{sql}"); Console.WriteLine($"========================================================================================================================================================================================================="); }; return client; }); #endregion #region 注册Redis { ConfigurationBinder.RegisterType(); ConfigurationBinder.RegisterType(); ConfigurationBinder.RegisterType(); ConfigurationBinder.RegisterType(); ConfigurationBinder.RegisterType(); } #endregion }); } } }