40

Tüm değeri veya özniteliği değiştirmek yerine belirli değer bölümlerini "dönüştürmek" için bir yol olup olmadığını bilen var mı? Örneğin, farklı web servisleri için URL'leri belirten birkaç appSettings girdim var. Bu girdiler, dev ortamlarda üretim ortamından biraz farklıdır. Bazıları diğerlerindenWeb.Config dönüştürmeyi kullanan gelişmiş görevler

<!-- DEV ENTRY --> 
<appSettings> 
<add key="serviceName1_WebsService_Url" value="http://wsServiceName1.dev.domain.com/v1.2.3.4/entryPoint.asmx" /> 
<add key="serviceName2_WebsService_Url" value="http://ma1-lab.lab1.domain.com/v1.2.3.4/entryPoint.asmx" /> 
</appSettings> 

<!-- PROD ENTRY --> 
<appSettings> 
<add key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" /> 
<add key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" /> 
</appSettings> 

yumruk girişinde, tek fark ".prod" dan ".dev" olduğu Bildirimi daha az önemsizdir. İkinci Girişte, alt alan farklıdır: "ws.ServiceName2"

Şimdiye kadar, ben Web.Release.Config böyle bir şey yapabileceğini biliyorum den "ma1-lab.lab1" :

<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName1_WebsService_Url" value="http://wsServiceName1.prod.domain.com/v1.2.3.4/entryPoint.asmx" /> 
<add xdt:Locator="Match(key)" xdt:Transform="SetAttributes(value)" key="serviceName2_WebsService_Url" value="http://ws.ServiceName2.domain.com/v1.2.3.4/entryPoint.asmx" /> 

Ancak, bu webcoder için Sürüm güncellenir her, benim web.config güncellemelerini simplfying amacı yendi hangi yanı Web.Release.Config güncellemek gerekir.

Ayrıca bu URL'yi farklı bölümlere ayırabildiğim ve bağımsız olarak güncelleyebileceğimi biliyorum, ancak hepsini tek bir tuşta kullanıyorum.

Kullanılabilir web.config Dönüşümlerini inceledim, ancak gerçekleştirmeye çalıştığım şeylere dikkat edilmiyor.

Bunlar bir referans olarak kullanıyorum web siteleri şunlardır:

Vishal Joshi's blog, MSDN Help ve Channel9 video

Herhangi bir yardım çok takdir!

-D

cevap

66
bunu yapabilirsiniz Nitekim olarak

ama düşündüğünüzden kadar kolay değildir. Kendi konfigürasyon dönüşümünüzü oluşturabilirsiniz. Bununla ilgili olarak http://sedodream.com/2010/09/09/ExtendingXMLWebconfigConfigTransformation.aspx numaralı telefondan çok detaylı bir blog yazısı yazdım. Ama burada hightlights şunlardır:

  • sınıf kitaplığı Proje oluşturma
  • Referans Web.Publishing.Tasks.dll (altında% Program Files (x86)% MSBuild \ Microsoft \ VisualStudio \ v10.0 \ Web klasörü) İthal yeni trans yapmak:
  • kullanın xdt iyi bilinen bir yerde uygulayın() yöntemini uygulamak
  • yerleştirin montaj Microsoft.Web.Publishing.Tasks.Transform sınıfını
  • uzatın
  • Kullanım'la formu bu İşte

    namespace CustomTransformType 
    { 
        using System; 
        using System.Text.RegularExpressions; 
        using System.Xml; 
        using Microsoft.Web.Publishing.Tasks; 
    
        public class AttributeRegexReplace : Transform 
        { 
         private string pattern; 
         private string replacement; 
         private string attributeName; 
    
         protected string AttributeName 
         { 
          get 
          { 
           if (this.attributeName == null) 
           { 
            this.attributeName = this.GetArgumentValue("Attribute"); 
           } 
           return this.attributeName; 
          } 
         } 
         protected string Pattern 
         { 
          get 
          { 
           if (this.pattern == null) 
           { 
            this.pattern = this.GetArgumentValue("Pattern"); 
           } 
    
           return pattern; 
          } 
         } 
    
         protected string Replacement 
         { 
          get 
          { 
           if (this.replacement == null) 
           { 
            this.replacement = this.GetArgumentValue("Replacement"); 
           } 
    
           return replacement; 
          } 
         } 
    
         protected string GetArgumentValue(string name) 
         { 
          // this extracts a value from the arguments provided 
          if (string.IsNullOrWhiteSpace(name)) 
          { throw new ArgumentNullException("name"); } 
    
          string result = null; 
          if (this.Arguments != null && this.Arguments.Count > 0) 
          { 
           foreach (string arg in this.Arguments) 
           { 
            if (!string.IsNullOrWhiteSpace(arg)) 
            { 
             string trimmedArg = arg.Trim(); 
             if (trimmedArg.ToUpperInvariant().StartsWith(name.ToUpperInvariant())) 
             { 
              int start = arg.IndexOf('\''); 
              int last = arg.LastIndexOf('\''); 
              if (start <= 0 || last <= 0 || last <= 0) 
              { 
               throw new ArgumentException("Expected two ['] characters"); 
              } 
    
              string value = trimmedArg.Substring(start, last - start); 
              if (value != null) 
              { 
               // remove any leading or trailing ' 
               value = value.Trim().TrimStart('\'').TrimStart('\''); 
              } 
              result = value; 
             } 
            } 
           } 
          } 
          return result; 
         } 
    
         protected override void Apply() 
         { 
          foreach (XmlAttribute att in this.TargetNode.Attributes) 
          { 
           if (string.Compare(att.Name, this.AttributeName, StringComparison.InvariantCultureIgnoreCase) == 0) 
           { 
            // get current value, perform the Regex 
            att.Value = Regex.Replace(att.Value, this.Pattern, this.Replacement); 
           } 
          } 
         } 
        } 
    } 
    

    yerine ağdır Yapmak oluşturulan sınıf İşte

dönüşümüdür.İşte

<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="one" value="one"/> 
    <add key="two" value="partial-replace-here-end"/> 
    <add key="three" value="three here"/> 
    </appSettings> 
</configuration> 

yapılandırma benim yapılandırma İşte

<?xml version="1.0"?> 

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 

    <xdt:Import path="C:\Program Files (x86)\MSBuild\Custom\CustomTransformType.dll" 
       namespace="CustomTransformType" /> 

    <appSettings> 
    <add key="one" value="one-replaced" 
     xdt:Transform="Replace" 
     xdt:Locator="Match(key)" /> 
    <add key="two" value="two-replaced" 
     xdt:Transform="AttributeRegexReplace(Attribute='value', Pattern='here',Replacement='REPLACED')" 
     xdt:Locator="Match(key)"/> 
    </appSettings> 
</configuration> 

Sadece bir güncelleme olarak
<?xml version="1.0"?> 
<configuration> 
    <appSettings> 
    <add key="one" value="one-replaced"/> 
    <add key="two" value="partial-replace-REPLACED-end"/> 
    <add key="three" value="three here"/> 
    </appSettings> 
</configuration> 
+0

Sayed, bu harika! Buna cevap vermek için zaman ayırdığınız için çok teşekkür ederim, umudumu kaybetmeye başlamıştım :) –

+0

Kutsal sürüngenler, bu muhteşem! Çok teşekkürler, dostum .. bu tam olarak ihtiyacım olan şey :) –

+0

Bunu yapmanın daha basit bir yolu olmadığı çok korkunç. :(Bunun için teşekkürler, çok yardımcı oldu. –

3

, Visual Studio 2013 kullanıyorsanız dönüşümden sonra sonucudur dosya dönüşümü olmaktadır , bunun yerine% Program Files (x86)% MSBuild \ Microsoft \ VisualStudio \ v12.0 \ Web \ Microsoft.Web.XmlTransform.dll başvurmalısınız.