.NET .config 地狱

发布于 2024-10-10 03:27:39 字数 1914 浏览 4 评论 0原文

我有以下项目:

  1. MVC
  2. 控制台应用程序
  3. 类库
  4. Windows 窗体应用程序
  5. COM 库

所有这些应用程序都需要使用单个配置文件。据我了解,app.config 文件适用于 Windows、控制台应用程序和类库,而 web.config 文件适用于 Web 项目。

所有这些项目都需要可以访问相同的配置。我读过建议使用计算机配置文件,但我们并不总是有权访问该文件,因此配置文件必须驻留在我们的解决方案中。

我不完全理解配置文件是如何构建的。目前,我编写了一个简单的项目,其中包含以下内容:

  1. 用于存储服务配置文件的类库。 A试图通过反思来做到这一点。
  2. 应从类库读取 app.config 的 Windows 应用程序。

当我执行以下代码时,我希望获得一个包含测试值的配置文件:

_applicationSettings = ConfigurationManager.OpenExeConfiguration(
                    System.Reflection.Assembly.GetAssembly(typeof(WCSConfiguration)).Location
                    ).AppSettings;    

我得到的是一个空的应用程序设置文件。

类库具有以下 App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestTextKey" value="TestTextValue"/>
  </appSettings> 
</configuration>

我尝试使用 .GetExecutingAssembly() 方法,我希望返回当前正在执行的代码的程序集。这不起作用,而是返回了 Windows 应用程序的程序集。

GetAssembly(type(WCSConfiguration)) 已返回正确的程序集,但是 bin/debug 目录中缺少配置文件。

我有一种感觉,要么我做的事情根本上是错误的,要么微软没有让这一点足够灵活。我也尝试在 MSDN 上搜索解释,但在我看来,这还没有得到很好的记录。

我还将 COM 保留为粗体,因为我不确定 COM 库是否可以使用任何配置文件。首先,我想让其他项目开始工作。

我知道这是很多信息。任何帮助将不胜感激。之前我们选择使用注册表,但事实证明这很糟糕,主要是因为在某些情况下无法访问注册表。此外,我们现在有多个版本的应用程序,并且在分支之间切换需要半个小时的工作:(

谢谢

编辑:

如果我将 dll 的配置部分添加到 app.config 中,这意味着这些设置将是只能从该应用程序中获得。如果我错了,请纠正我。我提供的示例是一个缩小版本。总共有大约十个 Windows 应用程序、一个 MVC 项目和一系列类库,所有这些都需要制作。该配置的使用

主要是连接字符串、不属于数据库的查找值和一些其他次要设置,此时主要关注的是连接字符串,每个版本都有一些次要版本。 时间

我想从中得到一个好的可行的解决方案,这样它就可以发布到网上,其他遇到同样问题的人就不会花费几天的

。我的故事: 使用 App.config 和 Web.config 来存储您自己的配置文件的位置。

编写简单的 XML 序列化程序来读/写配置和 DLL 以服务配置。

COM 对象是一个很长的故事,并且是通过“hack”实现的,因为 App.config 或 Web.config 在 COM DLL 中都不可用。

I have the following projects:

  1. MVC
  2. Console application
  3. Class library
  4. Windows forms application
  5. COM Library

All these applications need to use a single configuration file. As far as I understand, app.config files are for windows, console applications and class libraries when web.config are for the web projects.

The same configuration need to be accessible in all of these projects. I have read that it's suggested to use machine configuration file, but we won't always have access to that, therefore configuration files must reside within our solution.

I don't fully understand how the configuration files get build. Currently I wrote a simple project where I have the following:

  1. Class library to store for serving configuration files. A have attempted to do this through reflection.
  2. Windows application that should read the app.config from a class library.

When I execute the following code I expect to get a configuration file with test values:

_applicationSettings = ConfigurationManager.OpenExeConfiguration(
                    System.Reflection.Assembly.GetAssembly(typeof(WCSConfiguration)).Location
                    ).AppSettings;    

What I get instead is an empty application settings file.

Class library has the following App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestTextKey" value="TestTextValue"/>
  </appSettings> 
</configuration>

I have tried using .GetExecutingAssembly() method which I expect to return an assembly of a code that's currently being executed. This didn't work, instead it has returned the assembly of a Windows application.

GetAssembly(type(WCSConfiguration)) has returned a right assembly, however, the configuration file was missing in the bin/debug directory.

I have a feeling that either I'm doing something fundamentally wrong or Microsoft hasn't made this flexible enough. I have also tried to search MSDN for explanation, but this hasn't been documented well IMO.

I have also left COM in bold because I'm not sure whether any config files would be available to COM library at all. Firstly I would like to get other projects to work.

I understand that this is a lot of information. Any help would be greately appreciated. Previously we have chosen to use registry, but this has turned out to be nasty, mainly because access to registry is not available in some scenarios. Additionally we now have multiple versions of the applications and switching between branches is a half an hour job :(

Thank you

Edit:

If I add the dll's config sections to app.config that means that these settings will be available only from that application. Please correct me if I'm wrong. Example that I have provided is a scaled down version. In total there are about ten windows applications, a single MVC project and range of class libraries all of which need to make a use of that configuration.

Configuration settings are mostly connection strings, lookup values that do not belong in the database and few other minor settings. Main concern at this point are the connection strings. There are few minor releases of the application where each release points to a different database.

What I'd like to get out of this is a good workable solution so that it can be posted online and other people who come across the same problem won't spend days of their time.

Morale of the story IMO:
Use both App.config and Web.config to store location of your own configuration file.

Write simple XML serializer to read/write config and DLL for serving the configuration.

COM objects are a long story and were implemented with a "hack", since neither App.config or Web.config are available in COM DLLs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-10-17 03:27:39

注意 ConfigurationManager.OpenExeConfiguration 需要传递配置文件的文件名,而不是可执行文件的文件名。

您需要将 .config 附加到可执行文件的路径。要获取 exe 程序集,请使用 Assembly.GetEntryAssembly< /代码>

如果您想要在不全部位于同一 .NET 进程中的多段代码之间共享配置设置,我建议:

  • 将它们放在自己的 myStuff.config 中。
  • 在 .NET 代码中,使用 ConfigurationManager.OpenExeConfiguration 打开并访问 myStuff.config
  • 非 .NET 代码需要使用 XML 解析器来加载和读取设置。除非您的配置结构非常复杂,否则封装起来应该不会太难。
  • myStuff.config 的路径放入每个共享 .NET 应用程序配置的应用程序的 app.config 中。 (不是非 .NET 应用程序:取决于适用于该应用程序的内容。)

配置结构相同但设置针对每个应用程序的另一种方法是自定义配置部分。

Note ConfigurationManager.OpenExeConfiguration needs to be passed the filename of the config file, not the executable.

You'll need to append .config to the path of the executable. To get the exe assembly use Assembly.GetEntryAssembly.

If you have configuration settings you want to share across multiple pieces of code that are not all in the same .NET Process, I would suggest:

  • Put them in their own myStuff.config.
  • In .NET code use ConfigurationManager.OpenExeConfiguration to open and access myStuff.config.
  • Non-.NET code will need to use an XML parser to load and read the settings. Unless you configuration structures are very complex this shouldn't be too hard to encapsulate.
  • Put the path to myStuff.config in the app.config of each application sharing this configuration for .NET applications. (Not non-.NET applications: depends on what works for that application.)

Another approach, where the configuration structure is the same but the settings are per-application would be a custom configuration section.

几个一般要点——将 dll 的配置部分添加到 app.config 文件中,而不是依赖 dll 的配置文件来获取;并且 app.config 实际上在构建时被重命名为 .exe.config,因此您需要确保该名称的文件可用。

另外,您不必局限于使用默认的配置加载机制来配置您的应用程序。您可以创建自己的配置类,并且只需使用 XML 序列化即可随意反序列化和配置。

A couple of general points -- add the dll's config sections to the app.config file rather than relying on the dll's config file to get picked up; and app.config actually gets renamed to .exe.config on build, so you need to make sure a file of that name's available.

Also -- you're not constrained to using the default config loading mechanism to configure your application. You could create your own configuration classes and just use XML serialization to deserialize and configure at will.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文