代码之家  ›  专栏  ›  技术社区  ›  Ole Albers

使用VS中其他项目的配置

  •  0
  • Ole Albers  · 技术社区  · 10 年前

    我有一个包含web.Config的web项目。部署该配置时,On Deployment(到Azure)VS会修改该配置中的设置(ClientId)。

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

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

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

    2 回复  |  直到 10 年前
        1
  •  1
  •   blogprogramisty.net    10 年前

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

    第一:

    <appSettings configSource="appSettings.config"/>;

    所有物 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 您可以使用其他配置文件。我在测试项目中使用它来使用生产web.config

        2
  •  0
  •   Ole Albers    10 年前

    我找到了一个使用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)
    
    推荐文章