C# 加载配置文件
//加载配置文件
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
ConfigurationManager.Configuration = builder.Build();
#region 反射加载程序集dll到运行时,.net core 2.0不支持引用第三方DLL直接加载 TODO 升级2.0.3 或者Nuget加载
Assembly entry = Assembly.GetEntryAssembly();
string dir = Path.GetDirectoryName(entry.Location);
var dllPath = dir + "\\MongoRepository.Net45.dll";
AssemblyLoadContext.Default.LoadFromAssemblyPath(dllPath);
#endregion

//注册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();
//组件筛选使用
/* var assembiles = AppDomain.CurrentDomain.GetAssemblies()//GetReferencingAssemblies()
.Where(it => Regex.IsMatch(Path.GetFileNameWithoutExtension(it.Location) + ".dll",
$@"(OA|{assemblyNamePattens.Join("|")})\.[^\\]*\.dll")).ToArray(); */
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;
}
public static void SetContainer(ILifetimeScope container)
{
_container = container;
}
}
C# 加载配置文件的更多相关文章
- spring加载配置文件
spring加载配置文件 1.把applicationContext.xml直接放在WEB-INF/classes下,spring会采用默认的加载方式2.采用在web.xml中配置ContextLoa ...
- spring-自动加载配置文件\使用属性文件注入
在上一篇jsf环境搭建的基础上 , 加入spring框架 , 先看下目录结构 src/main/resources 这个source folder 放置web项目所需的主要配置,打包时,会自动打包到W ...
- powershell创建并加载配置文件
$pshome :powershell的主目录 $profile :显示 Windows PowerShell 配置文件的路径 test-path $profile :确定是否已经在系统上创建了 Wi ...
- java加载配置文件
有时候一些变量可能会变,但直接写在程序代码中不好,因为有时候需要改的时候要改动源代码,这时候可以使用配置文件来管理.比如数据库中的端口和密码. 1. 把.properties配置文件放在src目录下. ...
- 【Nutch2.2.1源代码分析之4】Nutch加载配置文件的方法
小结: (1)在nutch中,一般通过ToolRunner来运行hadoop job,此方法可以方便的通过ToolRunner.run(Configuration conf,Tool tool,Str ...
- Java中加载配置文件的集中方式,以及利用ClassLoader加载文件 .
我们往常进行文件的加载的时候 用到的都是 FileInputStream进行 文件的加载比如下面一个例子 : InputStream in=FileInputStream("1.prope ...
- java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...
- java动态加载配置文件(申明:来源于网络)
java动态加载配置文件 地址:http://blog.csdn.net/longvs/article/details/9361449
- nginx-启动|关闭|重新加载配置文件的命令
1.1 进入操作目录 D: cd D:\NginxTest\nginx-1.10.2 1.2 启动指令 nginx -c conf\nginx.conf 1.3 关闭指令 nginx -s stop ...
- tmux不自动加载配置文件.tmux.conf
/********************************************************************** * tmux不自动加载配置文件.tmux.conf * ...
随机推荐
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #3 如何编写内核模块
HACK #3 如何编写内核模块 本节将介绍向Linux内核中动态添加功能的结构—内核模块的编写方法.内核模块Linux内核是单内核(monolithic kernel),也就是所有的内核功能都集成在 ...
- string ids=aduuids.Aggregate("", (m, n) => m + n+",").TrimEnd(',');
string ids=aduuids.Aggregate("", (m, n) => m + n+",").TrimEnd(',');
- Windows下测量cpu利用率的类
方法参考网上的一段代码,把它改成了类以方便调用,很简单,但是可以一用,哈哈. cpu_usage.h #include <windows.h> class CpuUsage { publi ...
- leetcode204
public class Solution { public int CountPrimes(int n) { ) { ; } ]; ]; ; ; i < n; i++) { mark[i] = ...
- Guide to installing 3rd party JARs
Although rarely, but sometimes you will have 3rd party JARs that you need to put in your local repos ...
- 4-30 HTML 细节摘录
<b> 定义粗体文本. <big> 定义大号字. <em> 定义着重文字. <i> 定义斜体字. <small> 定义小号字. <s ...
- rook
https://github.com/rook/rook https://rook.github.io/docs/rook/master/ Rook是在云本地环境中运行的分布式存储系统的开源编排器. ...
- Python Modules
[Python Modules] 1. a module is a python source file. 2. a package is a directory with a __init__.py ...
- C# \/date(1498820611133+0800)\/ 转DateTime
开发中经常遇到日期转换问题,特别是做接口的时候,现在整理了下时间戳转为C#格式时间的方法: /// <summary> /// 时间戳转为C#格式时间 /// </summary&g ...
- bootstrop-datatime参数配置
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...