使用VS中其他项目的配置

时间:2016-04-06 07:58:27

标签: c# visual-studio configuration visual-studio-2015

我有一个包含Web.Config的Web项目。部署该配置时,在部署(到Azure)VS修改该配置内的设置(ClientId)。

<appSettings>
 <add key="ClientId" value="123456123-beef-face-96ce-12346dd8a108" />
</appSettings>

现在我还有一个控制台应用程序需要使用从Web应用程序部署创建的相同ClientId。更糟糕的是:我不仅需要设置中的值,而且还必须在App.config(appSettings / ClientId)中使用相同的结构。两个项目都在同一个解决方案中。

在构建/部署时,有没有办法使用相同的配置或自动将设置从一个配置复制到另一个配置?

2 个答案:

答案 0 :(得分:1)

有两种方法可以做到这一点:

首先:

configSource

属性[Test] public void Publish_News_Post_From_File_Test() { using (AppConfig.Change(@"otherFile.dll.config")) { //Now use other config file } } 使用其他文件中的设置

第二个(代码很好并且编译)

像这样使用:

public abstract class AppConfig : IDisposable
    {
        public static AppConfig Change(string path)
        {
            return new ChangeAppConfig(path);
        }

        public abstract void Dispose();

        private class ChangeAppConfig : AppConfig
        {
            private readonly string oldConfig =
                AppDomain.CurrentDomain.GetData("APP_CONFIG_FILE").ToString();

            private bool disposedValue;

            public ChangeAppConfig(string path)
            {
                AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", path);
                ResetConfigMechanism();
            }

            public override void Dispose()
            {
                if (!disposedValue)
                {
                    AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", oldConfig);
                    ResetConfigMechanism();

                    disposedValue = true;
                }
                GC.SuppressFinalize(this);
            }

            private static void ResetConfigMechanism()
            {
                typeof(ConfigurationManager)
                    .GetField("s_initState", BindingFlags.NonPublic |
                                             BindingFlags.Static)
                    .SetValue(null, 0);

                typeof(ConfigurationManager)
                    .GetField("s_configSystem", BindingFlags.NonPublic |
                                                BindingFlags.Static)
                    .SetValue(null, null);

                typeof(ConfigurationManager)
                    .Assembly.GetTypes()
                    .Where(x => x.FullName ==
                                "System.Configuration.ClientConfigPaths")
                    .First()
                    .GetField("s_current", BindingFlags.NonPublic |
                                           BindingFlags.Static)
                    .SetValue(null, null);
            }
        }
    }

源代码:

using

Public Function get_remoteTables(strPath As String) Dim objFSO As FileSystemObject Dim objFolder As Folder Dim objFiles As file Dim filedetails() As Variant Dim FileCount As Long Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder(strPath) ' Grab all the files in the subfolders For Each objSubFolder In objFolder.SubFolders 'print folder path Debug.Print (objSubFolder.Path) Set objFiles = objFSO.GetFolder(objSubFolder.Path).Files If Err.Number <> 0 Then FileCount = 0 Else FileCount = objFiles.Count End If get_Filenames (objSubFolder.Path) Next objSubFolder End Function Sub test() Call get_remoteTables("C:\temp\ ") End Sub stetment中您可以使用其他配置文件。我在测试项目中使用它来使用生产web.config

答案 1 :(得分:0)

我使用另一种使用Powershell的方法(作为后期构建)找到了一个解决方案:

$webconfig='C:\dev\mySolution\myWebProject\Web.config'
$appConfig='C:\dev\mySolution\myConsoleApp\App.config'

#Get Value from Web.Config:
$webconfigDoc=New-Object System.Xml.XmlDocument
$webconfigDoc.Load($webconfig)
$webappsettings=$webconfigDoc.SelectSingleNode('//appSettings')
$webclientIdNode=$webappsettings.SelectSingleNode("//add[@key='ClientId']")
$clientId=$webclientIdNode.value

#Set Value into App.Config:
$appconfigDoc=New-Object System.Xml.XmlDocument
$appconfigDoc.Load($appConfig)
$appsettings=$appconfigDoc.SelectSingleNode('//appSettings')
$appclientIdNode=$appsettings.SelectSingleNode("//add[@key='ClientId']")
$appclientIdNode.value=$clientId
$appconfigDoc.Save($appConfig)
相关问题