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;
}
}
上一篇: 带老婆看电影,也没什么毛病啊
下一篇: 20年春晚广告品牌的消亡史