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

75 lines
3.6 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<ContainerBuilder>(ConfigurationBinder =>
{
//ConfigurationBinder.RegisterType<UserManagerService>().As<IUserManagerService>();
#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<ISqlSugarClient>(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<SlaveConnectionConfig> {
// 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<RedisHashService>();
ConfigurationBinder.RegisterType<RedisListService>();
ConfigurationBinder.RegisterType<RedisSetService>();
ConfigurationBinder.RegisterType<RedisStringService>();
ConfigurationBinder.RegisterType<RedisZSetService>();
}
#endregion
});
}
}
}