2016-04-06 17 views
0

我有一個Web項目,其中包含一個Web.Config。部署該配置時,On Deployment(到Azure)VS修改該配置中的設置(ClientId)。使用配置從其他項目在VS

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

現在我還有一個控制檯應用程序,需要使用從Web應用程序通過部署創建的相同ClientId。更糟糕的是:我不僅僅需要設置中的值,而且還必須位於App.config(appSettings/ClientId)中的相同結構中。兩個項目都在同一個解決方案中。

有沒有辦法在構建/部署時使用相同的配置或自動將設置從一個配置複製到另一個配置?

+1

是。檢查

+0

很遺憾,這不起作用,因爲ClientId是自動寫入web.config。 –

回答

1

在兩個辦法做到這一點:

第一:

<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 stetment您可以使用其他的配置文件。我用它在測試項目中使用生產的web.config

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)