Domanda

Qualcuno sa se c'è un modo di "trasformare" specifiche sezioni di valori invece di sostituire l'intero valore o un attributo?

Per esempio, ho più voci AppSettings che specificano gli URL per i diversi webservices. Queste voci sono leggermente diversi nell'ambiente dev che l'ambiente di produzione. Alcuni sono meno banale di altri

<!-- 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>

Si noti che sulla voce pugno, l'unica differenza è "dev." Da ".prod" Sulla seconda voce, il sottodominio è diverso:. "MA1-lab.lab1 " " ws.ServiceName2"

Finora, so che posso fare qualcosa di simile nel Web.Release.Config:

<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" />

Tuttavia, ogni volta che la versione per che webservice viene aggiornato, avrei dovuto aggiornare il Web.Release.Config pure, che sconfigge lo scopo di simplfying miei aggiornamenti web.config.

So che potrei anche dividere l'URL in diverse sezioni e aggiornarli in modo indipendente, ma io preferirei avere tutto in un unico tasto.

Ho guardato attraverso le trasformazioni web.config disponibili, ma nothings sembra essere orientata towars quello che sto cercando di realizzare.

Questi sono i siti web che sto usando come riferimento:

Vishal Joshi del blog , Guida MSDN , e Video Channel9

Qualsiasi aiuto sarebbe molto apprezzato!

-D

È stato utile?

Soluzione

È un dato di fatto si può fare questo, ma non è così facile come si potrebbe pensare. È possibile creare la propria trasformazione config. Ho appena scritto un post molto dettagliato blog all'indirizzo http://sedodream.com/2010/09 /09/ExtendingXMLWebconfigConfigTransformation.aspx riguardo a questa. Ma ecco i hightlights:

  • Crea progetto Libreria di classi
  • Riferimento Web.Publishing.Tasks.dll (sotto% Programmi (x86)% MSBuild \ Microsoft cartella Web \ VisualStudio \ v10.0 \)
  • Estendere classe Microsoft.Web.Publishing.Tasks.Transform
  • Implementare il metodo Applica ()
  • Collocare il gruppo in una posizione ben nota
  • Usa xdt: Importa per rendere la nuova trasformare disponibili
  • Usa trasformare

Qui è la classe che ho creato per fare questo sostituire

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);
                }
            }
        }
    }
}

Ecco web.config

<?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>

Ecco il mio config file di trasformazione

<?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>

Ecco il risultato dopo la trasformazione

<?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>

Altri suggerimenti

Proprio come un aggiornamento, se si sta utilizzando Visual Studio 2013, si dovrebbe fare riferimento% Program Files (x86)% MSBuild \ Microsoft \ VisualStudio \ v12.0 \ Web \ Microsoft.Web.XmlTransform.dll invece.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top