C# 加载配置文件
作者:互联网
//加载配置文件
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
ConfigurationManager.Configuration = builder.Build();
//注册IOC=Autofac 类
public class IocManager
{
private static ILifetimeScope _container;
public static void Initialize(params string[] assemblyNamePattens)
{
ContainerBuilder builder = new ContainerBuilder();
_container = builder.Build();
Initialize(_container, assemblyNamePattens);
}
public static void Initialize(ILifetimeScope container, params string[] assemblyNamePattens)
{
container.Update(builder =>
{
//运行绝对路径=AppDomain.CurrentDomain.BaseDirectory
Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll")
.Where(it => Regex.IsMatch(it, $@"(OA|{assemblyNamePattens.Join("|")})\.[^\\]*\.dll"))
.Each(it => AppDomain.CurrentDomain.Load(Path.GetFileNameWithoutExtension(it)));
//注册IOC
var assembiles = AppDomain.CurrentDomain.GetAssemblies();
builder.RegisterAssemblyTypes(assembiles)
.Except<IDependencySingleton>()
.Except<IDependencyRequestSingleton>()
.As<IDependency>().AsSelf().AsImplementedInterfaces();
builder.RegisterAssemblyTypes(assembiles)
.Except<IDependencyRequestSingleton>()
.As<IDependencySingleton>().AsSelf().AsImplementedInterfaces().SingleInstance();
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.As<IDependencyRequestSingleton>().AsSelf().AsImplementedInterfaces()
.InstancePerLifetimeScope();
});
}
public static ILifetimeScope GetContainer()
{
return _container;
}
}
标签:container,配置文件,C#,builder,AppDomain,static,CurrentDomain,public,加载 来源: https://www.cnblogs.com/chxl800/p/10419994.html