using Autofac.Extensions.DependencyInjection;
namespace VOL.WebApi
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureKestrel(serverOptions =>
{
serverOptions.Limits.MaxRequestBodySize = 1024*1024*200;
// Set properties and call methods on options
});
webBuilder.UseKestrel().UseUrls("http://*:9091");//9091
//webBuilder.UseKestrel(o => { o.ListenAnyIP(1883, m => m.UseMqtt()); });//绑定MQTT服务端口
webBuilder.UseIIS();
webBuilder.UseStartup<Startup>();
}).UseServiceProviderFactory(new AutofacServiceProviderFactory());
}
}
Startup.cs
public void ConfigureContainer(ContainerBuilder builder)
{
Services.AddModule(builder, Configuration);
}
public static class AutofacContainerModuleExtension
{
// 获取实现了 IDependency 接口的类型 Type baseType = typeof(IDependency);
// 获取项目的运行时库 var compilationLibrary = DependencyContext.Default .RuntimeLibraries .Where(x => !x.Serviceable && x.Type == "project") .ToList();
// 加载运行时库中的程序集 List<Assembly> assemblyList = new List<Assembly>(); foreach (var _compilation in compilationLibrary) { try { assemblyList.Add(AssemblyLoadContext.Default.LoadFromAssemblyName(new AssemblyName(_compilation.Name))); } catch (Exception ex) { Console.WriteLine(_compilation.Name + ex.Message); } }
// 注册程序集中的类型到容器中 builder.RegisterAssemblyTypes(assemblyList.ToArray()) .Where(type => baseType.IsAssignableFrom(type) && !type.IsAbstract) .AsSelf().AsImplementedInterfaces() .InstancePerLifetimeScope();
// 注册 UserContext 类型到容器中 builder.RegisterType<UserContext>().InstancePerLifetimeScope();
// 注册 ActionObserver 类型到容器中 builder.RegisterType<ActionObserver>().InstancePerLifetimeScope();
// 注册 ObjectModelValidatorState 类型到容器中 builder.RegisterType<ObjectModelValidatorState>().InstancePerLifetimeScope();
// 获取数据库连接字符串 string connectionString = DBServerProvider.GetConnectionString(null);
// 根据数据库类型选择相应的数据库上下文并注册 if (DBType.Name == DbCurrentType.MySql.ToString()) { // 注册 MySql 数据库上下文 services.AddDbContextPool<VOLContext>(optionsBuilder => { optionsBuilder.UseMySql(connectionString); }, 64); } else if (DBType.Name == DbCurrentType.PgSql.ToString()) { // 注册 PgSql 数据库上下文 services.AddDbContextPool<VOLContext>(optionsBuilder => { optionsBuilder.UseNpgsql(connectionString); }, 64); } else { // 注册 SqlServer 数据库上下文 services.AddDbContextPool<VOLContext>(optionsBuilder => { optionsBuilder.UseSqlServer(connectionString); }, 64); }
// 根据配置选择使用 Redis 缓存服务或内存缓存服务并注册 if (AppSetting.UseRedis) { builder.RegisterType<RedisCacheService>().As<ICacheService>().SingleInstance(); } else { builder.RegisterType<MemoryCacheService>().As<ICacheService>().SingleInstance(); }
// 注册 Kafka 相关的服务 //if (AppSetting.Kafka.UseConsumer) // builder.RegisterType<KafkaConsumer<string, string>>().As<IKafkaConsumer<string, string>>().SingleInstance(); //if (AppSetting.Kafka.UseProducer) // builder.RegisterType<KafkaProducer<string, string>>().As<IKafkaProducer<string, string>>().SingleInstance();
return services; }
}
标签:容器,Autofac,RegisterType,builder,optionsBuilder,注册,webBuilder,public,注入 From: https://www.cnblogs.com/huangpy/p/17853361.html