C#DLL的配置文件 [英] C# DLL config file

查看:450
本文介绍了C#DLL的配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去一个app.config文件添加到我的DLL,但所有尝试都失败了。

Im trying to add an app.config file to my DLL, but all attempts have failed.

据MusicGenesis在把配置信息在DLL 这不应该是一个问题。所以,很显然,我做错了什么......

According to MusicGenesis in 'Putting configuration information in a DLL' this should not be a problem. So obviously I'm doing something wrong...

以下code应该从我的DLL返回我的ConnectionString:

The following code should return my ConnectionString from my DLL:

return ConfigurationManager.AppSettings["ConnectionString"];

然而,当我app.config文件复制到我的控制台应用程序,它工作正常。

However, when I copy the app.config file to my console application, it works fine.

任何想法?

推荐答案

这是不平凡创造一个.DLL的.NET配置文件,并有很好的理由。 .NET配置机制有很多内置到它的功能,以方便应用程序的轻松升级/更新,并保护安装的应用程序,从践踏每个人的配置文件。

It is not trivial to create a .NET configuration file for a .DLL, and for good reason. The .NET configuration mechanism has a lot of features built into it to facilitate easy upgrading/updating of the app, and to protect installed apps from trampling each others configuration files.

之间有一个DLL如何使用和应用的使用方式有很大的不同。你是不太可能有安装在同一台机器上为同一个用户的应用程序的多个副本。但是,你很可能有100个不同的应用程序或库中的所有利用一些.NET的DLL。

There is a big difference between how a DLL is used and how an application is used. You are unlikely to have multiple copies of an application installed on the same machine for the same user. But you may very well have 100 different apps or libraries all making use of some .NET DLL.

虽然有很少需要一个用户配置文件中分别跟踪应用程序的设置的不同副本,它的非常不太可能,你会希望所有的DLL的不同用法与共享配置彼此。出于这个原因,当你检索使用正常方法的配置对象,你回来的对象是联系在一起的应用程序域的配置您正在执行中,而不是特定的程序集。

Whereas there is rarely a need to track settings separately for different copies of an app within one user profile, it's very unlikely that you would want all of the different usages of a DLL to share configuration with each other. For this reason, when you retrieve a Configuration object using the "normal" method, the object you get back is tied to the configuration of the App Domain you are executing in, rather than the particular assembly.

应用程序域绑定到加载,你的code实际上是在组装根组装。在大多数情况下,这将是你的主要.EXE,这是加载了.DLL的组装。它可以在应用程序中旋转起来的其他应用程序域,但你必须明确地提供该应用程序域的根程序集是什么样的信息。

The App Domain is bound to the root assembly which loaded the assembly which your code is actually in. In most cases this will be the assembly of your main .EXE, which is what loaded up the .DLL. It is possible to spin up other app domains within an application, but you must explicitly provide information on what the root assembly of that app domain is.

由于这一切,创建库特定的配置文件的过程不那么方便了。这是你将用于创建不依赖于任何特定的程序集任意移动配置文件相同的过程,但要利用.NET的XML架构,配置节和配置元素机制等这需要创建一个 ExeConfigurationFileMap 对象,加载数据来标识配置文件将被存储,然后调用 ConfigurationManager中 OpenMappedExeConfiguration 来打开它到一个新的配置实例。这由自动路径生成机制提供的版本保护砍你了。

Because of all this, the procedure for creating a library-specific config file is not so convenient. It is the same process you would use for creating an arbitrary portable config file not tied to any particular assembly, but for which you want to make use of .NET's XML schema, config section and config element mechanisms, etc. This entails creating an ExeConfigurationFileMap object, loading in the data to identify where the config file will be stored, and then calling ConfigurationManager.OpenMappedExeConfiguration to open it up into a new Configuration instance. This will cut you off from the version protection offered by the automatic path generation mechanism.

据统计,你可能会使用这个库在内部设置,这是不太可能你有多个应用程序做任何一台机器/用户在使用它。 但是如果不是,有什么你应该记住。如果你使用一个全局配置文件为您的DLL,无论是引用它的应用程序,你需要担心的访问冲突。如果两个应用程序引用您的库发生在同一时间运行,每个都有自己的配置对象打开,那么当一个保存更改,将在下一次引起异常你尝试检索或其它应用程序保存数据。

Statistically speaking, you're probably using this library in an in-house setting, and it's unlikely you'll have multiple apps making use of it within any one machine/user. But if not, there is something you should keep in mind. If you use a single global config file for your DLL, regardless of the app that is referencing it, you need to worry about access conflicts. If two apps referencing your library happen to be running at the same time, each with their own Configuration object open, then when one saves changes, it will cause an exception next time you try to retrieve or save data in the other app.

解决这个得到的最安全,最简单的方法是要求它加载您的DLL的组件还提供其自身的一些信息,或者通过检查引用组件的应用程序域检测到它。用这个来建立某种形式的文件夹结构保持独立的用户配置文件为每个应用程序引用您的DLL。

The safest and simplest way of getting around this is to require that the assembly which is loading your DLL also provide some information about itself, or to detect it by examining the App Domain of the referencing assembly. Use this to create some sort of folder structure for keeping separate user config files for each app referencing your DLL.

如果你的某种你想拥有的全局设置的DLL无论身在何处引用它,你需要确定你的位置了它,而不是.NET会自动计算出一个合适的。您还需要保持侵略性有关管理访问文件。你需要缓存尽可能,保持配置实例只左右,只要它需要加载或保存,立即开放之前和之后立即处理。最后,你需要一个锁定机制来保护文件,而它正在由使用该库的应用程序之一编辑。

If you are certain you want to have global settings for your DLL no matter where it is referenced, you'll need to determine your location for it rather than .NET figuring out an appropriate one automatically. You'll also need to be aggressive about managing access to the file. You'll need to cache as much as possible, keeping the Configuration instance around ONLY as long as it takes to load or to save, opening immediately before and disposing immediately after. And finally, you'll need a lock mechanism to protect the file while it's being edited by one of the apps that use the library.

这篇关于C#DLL的配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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