2010-06-17 6 views
10

Je souhaite utiliser la nouvelle fonctionnalité de transformation web2011 VS2010 pour modifier la chaîne de connexion dans la configuration nhibernate de mon fichier web.config. L'extrait pertinent est quelque chose comme ceci:Comment appliquer la transformation VS2010 web.config à un élément avec un attribut namespace?

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" /> 
    </configSections> 

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
     <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property> 
     <property name="connection.connection_string">(test connection string)</property> 
     <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
... 

J'ai essayé la transformation suivante sans succès:

<?xml version="1.0"?> 
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > 
     <session-factory> 
      <property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</property> 
     </session-factory> 
    </hibernate-configuration> 
</configuration> 

Le problème semble être dans l'attribut xmlns de l'élément NHibernate configuration.

Quelle devrait être la bonne transformation à remplacer (tester la chaîne de connexion) avec (chaîne de connexion de production) pendant le déploiement?

Répondre

7

La réponse est peut-être un peu en retard, mais depuis que je avais besoin cela aussi, je me suis dit que je posterais une réponse qui a fonctionné pour moi dans les quelqu'un d'autre l'événement tombe sur cette question.

Vous devez utiliser le xdt: Locator en combinaison avec une expression xpath pour obtenir le bon noeud. Donc, quelque chose comme ça devrait fonctionner.

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
     <property name="connection.connection_string" xdt:Locator="XPath(//*[local-name()='hibernate-configuration']//*[local-name()='property'][@name='connection.connection_string'])" xdt:Transform="Replace">(production connection string)</property> 
    </session-factory> 
</hibernate-configuration> 

Il peut y avoir une meilleure expression XPath, mais c'est ce qui a fonctionné pour moi.

Le seul problème, qui n'est pas si grave, est que le nœud remplacé aura un espace de noms redéclaré sur le nœud. Ainsi, le nœud remplacé ressemblera à ceci dans la sortie finale.

<property name="connection.connection_string" xmlns="urn:nhibernate-configuration-2.2">(production connection string)</property> 
0

Étant donné que session-factory contient une collection d'éléments enfants, vous devez lui indiquer quel enfant remplacer à l'aide du localisateur de correspondance.

<?xml version="1.0"?> 
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
     <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
      <session-factory> 
       <property name="connection.connection_string" xdt:Transform="Replace" xdt:Locator="Match(name)>(production connection string)</property> 
      </session-factory> 
     </hibernate-configuration> 
    </configuration> 
+0

J'ai déjà essayé, mais l'étape de transformation ne peut pas localiser même le noeud « mise en veille prolongée configuration » ... très probablement en raison de son attribut xmlns . – gschuager

+0

Je sais que je saisis des pailles ici, mais la dernière chose que j'essaierais avant d'abandonner serait d'éliminer cet espace blanc supplémentaire dans l'élément de configuration hibernate afin que les configs correspondent exactement. J'ai modifié mon code ci-dessus pour refléter ce dont je parle. –

15

J'ai récemment rencontré le même problème - il a été résolu en plaçant préfixes d'espace de noms explicites dans le fichier de transformation

<configuration 
       xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" 
       xmlns:hib="urn:nhibernate-configuration-2.2" 
       > 
    <hib:hibernate-configuration> 
     <hib:session-factory> 
      <hib:property name="connection.connection_string" xdt:Transform="Replace">(production connection string)</hib:property> 
     </hib:session-factory> 
    </hib:hibernate-configuration> 
</configuration> 

Le fichier web.config transformé résultant a été heureusement libre des préfixes d'espace de noms (c. il a laissé la déclaration d'espace de noms nhibernate au même endroit que dans le fichier original web.config et correctement nommé tous les noeuds)

+0

J'ai également besoin d'ajouter Locator match pour obtenir le mien de travailler: Source de données = Production.db

+1

J'ai créé un outil pour tester la transformation config, peut-être que cela peut être une aide: http://webconfigtransformationtester.apphb.com/ – friism

+0

Cela fonctionne pour moi, mais l'élément remplacé a le 'xmlns: hib = "..." 'injecté :( –

1

Si tout ce que vous essayez de faire est de transformer e chaîne de connexion, n'utilisez pas le mécanisme de transformation. Au lieu de cela, dans votre web.config ou app.config, référence cette propriété

connection.connection_string_name 

au lieu de celui-ci:

connection.connection_string 

Cela vous permet de référencer la chaîne de connexion définie dans la section ConnectionStrings, qui est transformé de la manière habituelle.

par exemple dans web.config, utilisez ce code:

<connectionStrings> 
    <add name="DefaultConnection" connectionString="server=MYSERVER; Integrated Security=SSPI; database=MYDATABASE"/> 
</connectionStrings> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
    <session-factory> 
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> 
    <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string_name">DefaultConnection</property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> 
    <property name="current_session_context_class">web</property> 
    <property name="show_sql">true</property> 
    </session-factory> 
</hibernate-configuration> 
Questions connexes