dll 的 App.config [英] App.config for dll

查看:35
本文介绍了dll 的 App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个引擎",可以动态加载 dll(无论位于某个目录中的任何内容)并通过反射从它们调用 Workflow 类.

我们现在有一些需要访问数据库的新工作流,所以我想我会在 dll 目录中放置一个配置文件.

但由于某种原因,我的工作流程看不到配置文件.

<预><代码><配置><应用设置><add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;"/></appSettings></配置>

给定上面的配置文件,下面的代码打印一个空字符串:

Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);

我想我想要的只是指定一个配置文件名,但我在这里遇到了问题.我只是没有得到结果.有大佬指点一下吗?

解决方案

如果用于读取 AppSettings 的代码示例在您的 DLL 中,那么它将尝试读取应用程序的配置文件而不是 DLL 的配置文件.这是因为您正在使用反射来执行代码.

We have an "engine" that loads dlls dynamically (whatever is located in a certain directory) and calls Workflow classes from them by way of reflection.

We now have some new Workflows that require access to a database, so I figured that I would put a config file in the dll directory.

But for some reason my Workflows just don't see the config file.

<configuration>
  <appSettings>
      <add key="ConnectString" value="Data Source=officeserver;Database=mydatabase;User ID=officeuser;Password=officeuser;" />
  </appSettings>
</configuration>

Given the above config file, the following code prints an empty string:

Console.WriteLine(ConfigurationManager.AppSettings["ConnectString"]);

I think what I want is to just specify a config filename, but I'm having problems here. I'm just not getting results. Anyone have any pointers?

解决方案

If your code sample for reading the AppSettings is in your DLL, then it will attempt to read the config file for the application and not the config file for the DLL. This is because you're using Reflection to execute the code.

这篇关于dll 的 App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆