14

是否有任何工具或Visual Studio 2010擴展允許我在不必發佈整個項目的情況下查看配置文件轉換的輸出?執行轉換的過程是否可直接調用?用於預覽配置文件轉換的工具


編輯

多一點谷歌搜索後,我碰到this

第4步:生成從「轉移」環境的新轉換的web.config文件命令行

通過 打開Visual Studio命令提示符將開始 - >程序 - > 的Visual Studio V10.0 - > Visual Studio的 工具 - > Visual Studio的10.0命令提示符

類型「的MSBuild「應用程序的路徑 項目文件(的.csproj/.vbproj)」 /噸:TransformWebConfig /p:配置=轉移」和命中 輸入,如下所示:

commandline web.config transformation

一旦轉化成功 web.config中的「暫存」 配置將在OBJ存儲 - >你的項目根目錄下的臨時文件夾(在解決方案資源管理器,你可以 訪問此文件夾,首先取消隱藏 隱藏文件):

transformed web.config

  • 在Solution Explorer中單擊按鈕顯示隱藏文件
  • 打開的OBJ文件夾
  • 導航到主動配置(在我們的銅rrent情況下,它是「分段」)
  • 您可以找到轉化的web.config有

現在,您可以驗證新 分期web.config文件中生成具有 更改的連接字符串部分。

來源:Web Deployment: Web.Config Transformation

這是不是真的對我來說是完美的解決方案,因爲它仍然需要與他發佈命令至少建造整個項目爲。如果有人知道如何通過MSBuild命令跳過構建步驟,這將有所幫助(儘管聽起來不太可能)。

編輯2

我也CodePlex上,它提供了一些不錯的功能,延長改造過程中發現這個Config Transformation Tool。這個工具是我見過的功能中最接近的功能,它將是開發創建預覽的擴展的一個很好的起點。它使用Microsoft.Web.Publishing.Tasks庫來執行轉換,而不依賴於構建實際項目。

回答

7

您可以通過使用MSBuild任務使用的相同對象來轉換配置文件,完全繞過MSBuild。 Web配置轉換邏輯包含在Microsoft.Web.Publishing.Tasks庫中。

以下代碼片段來自簡單的類庫,引用Microsoft.Web.Publishing.Tasks庫(它安裝在我的機器上的C:\ Program Files文件(x86)\ MSBuild \ Microsoft \ VisualStudio \ v10中。 0 \網絡)。

示例加載源文檔並轉換,應用轉換並將結果寫入新文件。

using System; 
using Microsoft.Web.Publishing.Tasks; 

// ... 

var xmlTarget = new XmlTransformableDocument(); 
xmlTarget.PreserveWhitespace = true; 
xmlTarget.Load("Web.config"); 

var xmlTransform = new XmlTransformation("Web.Release.config"); 

if (xmlTransform.Apply(xmlTarget)) 
    xmlTarget.Save("Web.Transformed.config"); 
else 
    Console.WriteLine("Unable to apply transform."); 

隨着一點點的創造力,這種簡單的解決方案可以被集成到Visual Studio的插件,也許在web.config文件的上下文菜單項。至少,您可以製作一個控制檯實用程序或腳本來生成預覽。

祝你好運!

+0

這基本上是我的 「編輯2」 段落的擴展。我覺得至少有一些需要這樣的工具,我開始把自己的作品放在一起。如果幸運的話,我希望在不久的將來在擴展庫上發佈一些東西。謝謝你的反饋! – 2010-09-29 16:10:50

+0

@Nathan:祝你好運。我期待着看到它。 CodePlex工具通過調用MSBuild工作。對於自定義工具,您可能希望直接使用庫,如圖所示。幫我一個忙,並在完成後添加評論。祝你好運! – kbrimington 2010-09-29 18:33:16

+0

我最初以爲它通過調用MSBuild工作,但在瀏覽源代碼後,我意識到他正在使用上面提到的庫。從它的外觀來看,構建擴展的功能原型應該不是什麼難事。 – 2010-09-29 23:07:51

12

的SlowCheetah VS加載的visualstudiogallery讓你預覽轉換結果

+1

是的SlowCheetah使這成爲一個右鍵點擊,所以它的完美 – 2012-02-11 19:57:12

+0

在經過了半小時的變換鬥爭後,我想谷歌的變換工具,來到這裏,安裝它,它顯示我的問題馬上。 2分鐘內修復。除了我的「必備之物,我以前怎麼沒有管理它?」工具包。 – 2015-08-28 10:18:43

2

舊的職位,但想到我會分享我曾與一個快速谷歌發現(對於那些可能沒有發現它或者試圖在這裏第一次):

Web.config Transformation Tester - By AppHarbor
只是在你原來的XML與XML轉換沿和看到的結果瞬間。

此外,它是open source任何人誰感興趣。

0

只是爲了延長這一點。 我需要上面討論的內容。只能運行轉換。 然後將其綁定到我的構建過程中,碰巧是TeamCity。

您需要使用Microsoft.Web.Publishing.Tasks,您可以使用Nuget進行粉碎。那麼,我在VS2013,所以我可以。否則我相信你可以獲得dll。

寫了一個簡單的控制檯應用程序。你可能會覺得它很有用。

的Program.cs

using System; 

namespace WebConfigTransform 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      if (args.Length != 3) 
      { 
       Console.WriteLine("Config Gen ... usage -source -transform -destination"); 
       Environment.Exit(-1); 
      } 

      Transform t = new Transform(args[0], args[1], args[2]); 
      t.Run(); 
     } 
    } 
} 

Transform.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Security; 
using System.Security.Permissions; 
using Microsoft.Web.XmlTransform; 

namespace WebConfigTransform 
{ 
    class Transform 
    { 
     private readonly string m_source; 
     private readonly string m_transform; 
     private readonly string m_destination; 

     public Transform(string source, string transform, string destination) 
     { 
      m_source = source; 
      m_transform = transform; 
      m_destination = destination; 
     } 

     private void TransformFiles() 
     { 
      var xmlTarget = new XmlTransformableDocument(); 
      xmlTarget.PreserveWhitespace = true; 
      xmlTarget.Load(m_source); 
      var xmlTransform = new XmlTransformation(m_transform); 

      if (xmlTransform.Apply(xmlTarget)) 
       xmlTarget.Save(m_destination); 
      else 
      { 
       Console.WriteLine("Unable to apply transform."); 
       Environment.Exit(-1); 
      } 
     } 

     private void CheckPermissions() 
     { 
      string directoryName = m_destination; 
      PermissionSet permissionSet = new PermissionSet(PermissionState.None); 
      FileIOPermission writePermission = new FileIOPermission(FileIOPermissionAccess.Write, directoryName); 
      permissionSet.AddPermission(writePermission); 
      if (!(permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet))) 
      { 
       Console.WriteLine("Cannot write to file : " + m_destination); 
       Environment.Exit(-1); 
      } 
     } 

     private void CheckFileExistance() 
     { 
      List<string> ls = new List<string>(); 
      ls.Add(m_source); 
      ls.Add(m_transform); 
      foreach (string item in ls) 
      { 
       if (!File.Exists(item)) 
       { 
        Console.WriteLine("Cannot locate file : " + item); 
        Environment.Exit(-1); 
       } 
      } 
     } 

     public void Run() 
     { 
      CheckFileExistance(); 
      CheckPermissions(); 
      TransformFiles(); 
     } 
    } 
}