.NET 配置文件 configSource 在应用程序目录文件夹之外
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
.NET Config Files configSource outside the application directory folder
提问by Robert MacLean
I have two applications one a console application and the other an ASP.NET app. They both need to know the same appSettings and connectionStrings. So ideally I would like to use the configSource property of app.config/web.config files to point that to a central location. For example
我有两个应用程序,一个是控制台应用程序,另一个是 ASP.NET 应用程序。他们都需要知道相同的 appSettings 和 connectionStrings。所以理想情况下,我想使用 app.config/web.config 文件的 configSource 属性将其指向一个中心位置。例如
<connectionStrings configSource="D:\connectionStrings.config"/>
<appSettings configSource="D:\appSettings.config"/>
That however fails with an error:
然而,失败并出现错误:
The configSource attribute is invalid.: The configSource 'D:\appSettings.config' is invalid. It must refer to a file in the same directory or in a subdirectory as the configuration file.
configSource 属性无效。: configSource 'D:\appSettings.config' 无效。它必须引用与配置文件位于同一目录或子目录中的文件。
Is there anyway to still use the configuration managers appSettings/connectionStrings and get the values from an external location?
I'm happy with having to add code to do it, but I don't want to have to replace the entire configuration manager system.
无论如何仍然使用配置管理器 appSettings/connectionStrings 并从外部位置获取值?
我很高兴必须添加代码来完成它,但我不想更换整个配置管理器系统。
回答by
Another solution is simply to add the configuration file in all your projects as a link instead of actually copying the file to your projects. Then set the "Build Action" of the file to "Content" and "Copy to Output Directory" to "Copy if newer" and when you compile the project you will have the file in the output directory.
另一种解决方案是将配置文件作为链接添加到所有项目中,而不是实际将文件复制到项目中。然后将文件的“构建操作”设置为“内容”,将“复制到输出目录”设置为“如果更新则复制”,当您编译项目时,您将在输出目录中拥有该文件。
To add the file as a link in the "Add Existing Item" dialog box, there is an Add button with a dropdown. Choose "Add as link" from the dropdown on the Add button to complete the process.
要将文件作为链接添加到“添加现有项目”对话框中,有一个带有下拉菜单的添加按钮。从“添加”按钮的下拉菜单中选择“添加为链接”以完成该过程。
回答by Robert MacLean
Under appSettings you can use file= instead of configSource=
在 appSettings 下,您可以使用 file= 而不是 configSource=
回答by Iain M Norman
It seems that's that is the way it is. configSource must be in the same folder or deeper.
似乎就是这样。configSource 必须位于同一文件夹或更深的文件夹中。
You could, although I'm not sure you should, use an NTFS hardlink. [mad grin]
你可以,虽然我不确定你是否应该,使用 NTFS 硬链接。[疯狂的笑容]
回答by Serexx
Visual Studio 2015
视觉工作室 2015
If you are having this issue with Web.Config the accepted answer is correct but just to expand since this had me giving myself the face-palm:
如果您在使用 Web.Config 时遇到此问题,则接受的答案是正确的,但只是为了扩展,因为这让我给了自己面子:
When you add a .config file to your project using 'Add As Link' and then set the link's Copy property to 'Copy If Newer' or 'Copy Always', then the physical file will be copied to the /bin folder.
当您使用“添加为链接”将 .config 文件添加到您的项目,然后将链接的复制属性设置为“如果较新则复制”或“始终复制”时,物理文件将被复制到 /bin 文件夹。
Thus, when you have a config section defined in Web.Config like this:
因此,当您在 Web.Config 中定义了一个配置部分时,如下所示:
<section name="mySpecialConfig" type="System.Configuration.AppSettingsSection" requirePermission="false" />
then you must define the related config element like this:
那么你必须像这样定义相关的配置元素:
<mySpecialConfig configSource="bin\MySpecialConfig.config">
</mySpecialConfig>
such that the configSource points to the physical bin\MySpecialConfig.config file not to the linkAlso, note that the path is a relativephysical path.
这样 configSource 指向物理 bin\MySpecialConfig.config 文件而不是链接。另外,请注意该路径是一个相对物理路径。
That may seem ridiculously obvious but if you haven't done this before the physical file is not yet in the \bin folder so it may not click right away.
这可能看起来非常明显,但如果您在物理文件还没有在 \bin 文件夹中之前没有这样做,那么它可能不会立即单击。
回答by Richard
You can load configuration from an arbitrary location, butit won't be available via the static properties of ConfigurationManager:
您可以从任意位置加载配置,但不能通过 ConfigurationManager 的静态属性使用:
Configuration myConfig = ConfigurationManager.OpenExeConfiguration(path)
(There is an overload that allows multuple files to be specified, to support default/user-roaming/user-local hierarchy.)
(有一个重载允许指定多个文件,以支持默认/用户漫游/用户本地层次结构。)
Losing the static properties means all the code needs to be aware of the different configuration.
丢失静态属性意味着所有代码都需要了解不同的配置。
回答by Scott
In the case of connection strings, it is indeed possible to point to a shared file. If the shared file is on a network UNC, it requires administrative privileges on the machine where the app will be hosted.
在连接字符串的情况下,确实可以指向共享文件。如果共享文件位于网络 UNC 上,则需要在托管应用程序的计算机上具有管理权限。
Solution: In your web.config, use configSource to point to a local config file. Due to .Net restrictions, this must be at or below the level of the root config file. I just point to a file in the app folder itself:
解决方案:在您的 web.config 中,使用 configSource 指向本地配置文件。由于 .Net 限制,这必须处于或低于根配置文件的级别。我只是指向 app 文件夹中的一个文件:
<connectionStrings configSource="ConnectionStrings.config" />
In a shared location that is accessible by the application pool user, add the config file containing shared connection strings. This file must not contain any xml other than the connectionStrings section itself. The shared file, ConnectionStrings.config, looks like this:
在应用程序池用户可访问的共享位置,添加包含共享连接字符串的配置文件。除了 connectionStrings 部分本身,此文件不得包含任何 xml。共享文件 ConnectionStrings.config 如下所示:
<connectionStrings>
<clear/>
<add name="connString1" connectionString="connString1 info goes here"/>
<add name="connString2" connectionString="connString2 info goes here"/>
</connectionStrings>
Now the trick. Create a Windows symbolic link in your app folder pointing to the external, shared config file. You will need admin privileges to do this:
现在的伎俩。在您的应用程序文件夹中创建一个指向外部共享配置文件的 Windows 符号链接。您将需要管理员权限才能执行此操作:
mklink ConnectionStrings.config \someServer\someShare\someFolder\ConnectionStrings.config
We have just outsmarted .Net. The Configuration system will use the configSource setting to find connection strings in a local file called ConnectionStrings.config. The symbolic link looks like a file to .Net, and the symbolic link resolves to the shared configuration file.
我们刚刚超越了 .Net。配置系统将使用 configSource 设置在名为 ConnectionStrings.config 的本地文件中查找连接字符串。符号链接看起来像 .Net 的文件,符号链接解析为共享配置文件。
Caveats: Changes to the shared file do not automatically trigger an app restart in .Net. In the case of IIS, the web site or app pool will need to be restarted manually.
警告:对共享文件的更改不会自动触发 .Net 中的应用程序重新启动。对于 IIS,需要手动重新启动网站或应用程序池。
Due to the need for administrative privileges to create the symbolic link, this approach may not work for everybody. There are two related alternatives that may work if the shared file is on the same logical drive - hard links and junctions. See this discussionand this discussionfor more information.
由于创建符号链接需要管理权限,因此这种方法可能不适用于所有人。如果共享文件位于同一逻辑驱动器上,则有两种相关的替代方法可能会起作用 - 硬链接和联结。有关更多信息,请参阅此讨论和此讨论。
回答by freggel
You can place both settings in the machine.configand then they are available for all you applications on the server.
您可以将这两个设置放在machine.config 中,然后它们可用于服务器上的所有应用程序。
回答by Robert MacLean
The solution I found worked best was to put the "shared" config files in a central files and then use a pre-build event in Visual Studio to copy them to a relative folder of each project which needed it.
我发现最有效的解决方案是将“共享”配置文件放在中央文件中,然后在 Visual Studio 中使用预构建事件将它们复制到每个需要它的项目的相关文件夹中。
回答by marcel_g
I had quite a struggle with this issue, but I found a good solution for it here: test run with external config
我在这个问题上遇到了很多困难,但我在这里找到了一个很好的解决方案:test run with external config
(You can direct the test run to copy files and directories into the test run directory by editing the .testrunconfig file.)
(您可以通过编辑 .testrunconfig 文件指示测试运行将文件和目录复制到测试运行目录中。)
Although why the unit test type project can get config settings from its own app.config, but not be able to load referenced config files like a normal app.config is kind of baffling to me. I'd call it a bug because you would expect a test project app.config to behave the same way that the application's app.config behaves, but it doesn't.
虽然为什么单元测试类型的项目可以从它自己的 app.config 中获取配置设置,但不能像普通的 app.config 一样加载引用的配置文件,这对我来说有点莫名其妙。我将其称为错误,因为您希望测试项目 app.config 的行为方式与应用程序的 app.config 行为方式相同,但事实并非如此。
回答by alexs
You can use the fileattribute rather than configSource
您可以使用file属性而不是configSource
There's a good article hereon it
有一个很好的文章在这里就可以了
This allows you to specify a relative path like so
这允许您像这样指定相对路径
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings file="..\..\..\..\..\..\ExternalFile.config"></appSettings>
</configuration>
The path is relative to the output directory.
路径是相对于输出目录的。
Then in ExternalFile.config you just add the appSettingssection
然后在 ExternalFile.config 中,您只需添加该appSettings部分
<appSettings>
<add key="SomeKey" value="alue"/>
</appSettings>

