将dll的配置文件移动到调用dll的应用程序 [英] moving the config files for a dll to the app that calls the dll

查看:224
本文介绍了将dll的配置文件移动到调用dll的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有搜索功能的网络应用程序。搜索算法被编译成单独的dll。在搜索算法的C#代码中,我使用设置文件中保存的字符串来指向搜索索引所在的目录。一旦搜索代码被编译,设置信息就被合并到Search.dll中的 Search.dll.config 中。现在在我的网络应用程序中,我将Search.dll添加到引用中。该配置文件未添加到Web应用程序。然而,网络应用程序运行正常,并且知道文件在哪里。因为在 Settings.Designer 内部,如果配置文件不存在,它将使用 DefaultSettingValueAttribute 分配默认值。



如何将$ code> Search.dll.config 添加到我的网络应用程序中,以便操作员可以更改索引文件的位置在服务器上需要吗?



谢谢



编辑:



将配置文件添加到我的部署文件夹。但ASP.NET将dll放在C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root ......目录下,并且配置文件不不会被复制在那里所以在这一点上,我不知道如何将配置文件与我的代码一起包含。



感谢您的帮助。



注意:



我已经使用以下代码将配置文件的值获取到应用程序中。然而,这取决于dll和配置文件在同一个文件夹,我不知道如何完成。

  var executedAssembly = System.Reflection.Assembly.GetExecutingAssembly(); 
var location = executionAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
var config = ConfigurationManager.OpenExeConfiguration(location);
var sections = config.Sections; //这个数目是21
ConfigurationSectionGroup csg = config.GetSectionGroup(applicationSettings);
ConfigurationSectionCollection csc = csg.Sections;
ConfigurationSection cs = csc.Get(Search.Properties.Settings);


解决方案

最好的办法是将配置添加到网络项目直接。 .NET并不真正支持与您尝试的方式相关联的库的配置;这是设计的。您的库的其他用户可能需要不同的配置。将文件标记为应该被复制到输出文件夹的内容。



编辑:



在文件属性中将构建操作设置为内容和复制到输出目录为复制(如果较新)。您可以使用 HttpRuntime.BinDirectory 发现bin文件夹中的文件。您可能希望将此位置传递到您的库,而不是让库假定它正在一个Web项目中运行。



或者,只需将web需要的配置(配置文件也有可能将设置分解为单独的文件)。



最后,您可以考虑将文件嵌入到资源中。



编辑:



查看您正在使用的代码,只需将其移动到web.config中即可。更容易和惯用。一旦它在web.config中,只需使用 ConfigurationManager.GetSection()来阅读您的部分。



顺便提一句,自由配置部分设计器,使创建类非常容易支持自定义配置部分。在VS扩展在线画廊中查看:



http://visualstudiogallery.msdn.microsoft.com/2a69f74e-83df-4eb0-8cac-cd83b451cd1d?SRC=VSIDE


I have a web app that has search functionality. The search algorithm is compiled to a separate dll. In the C# code for the search algorithm, I am using strings held in a settings file to point to the directory where the search index resides. Once the search code is compiled, the settings info is incorporated in Search.dll.config which is put in the bin directory along with Search.dll. Now in my web app, I add Search.dll to the references. The config file is not added into the web app. However the web app runs fine and knows where the file is. Because inside Settings.Designer it uses the DefaultSettingValueAttribute to assign a default if the config file is not there.

How do I also add Search.dll.config to my web app so the operator can change the location of the index files on the server as need be?

Thanks

EDIT:

I tried to add the config file to my deployment folder. But ASP.NET puts the dlls in a directory at C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root...... and the config file doesn't get copied there. So at this point I have no idea how to include the config file with my code.

Thanks for your help.

Note:

I have been using the following code to get the values of the config file into the app. However, it depends on the dll and the config file to be in the same folder, which I do not know how to accomplish.

    var executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
    var location = executingAssembly.Location; //C:\MyApp\bin\Debug\Search.dll
    var config = ConfigurationManager.OpenExeConfiguration(location);
    var sections = config.Sections; //count of this is 21
    ConfigurationSectionGroup csg = config.GetSectionGroup("applicationSettings");
    ConfigurationSectionCollection csc = csg.Sections;
    ConfigurationSection cs = csc.Get("Search.Properties.Settings");

解决方案

Your best bet is to add the config to the web project directly. .NET doesn't really support configuration associated with a library in the way you are attempting; and this is by design. Other users of your library may need different configuration. Mark the file as being content that should be copied to the output folder.

EDIT:

To do this set "Build Action" to "Content" and "Copy to Output Directory" as "Copy if newer" in the file properties. You can discover the file in the bin folder using HttpRuntime.BinDirectory. You may want to pass this location to your library rather than have the library assume it's running in a web project.

Alternatively, just embed the config you need in web.config (config files have a facility to break out settings into a separate file as well).

Finally, you could consider embedding the file as a resource.

EDIT:

Looking at the code you are using, just move it into web.config. Much easier, and idiomatic. Once it's in web.config just use ConfigurationManager.GetSection() to read your section.

Incidentally, there's a free configuration section designer that makes creating the classes to support custom configuration sections very easy. Look in the VS Extensions Online Gallery here:

http://visualstudiogallery.msdn.microsoft.com/2a69f74e-83df-4eb0-8cac-cd83b451cd1d?SRC=VSIDE

这篇关于将dll的配置文件移动到调用dll的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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