web.config transformations not applied to special section
I am new to web.config transformations but have it working for my connection string. However, my transforms for a custom section (nhibernate) are not being applied. Here's the transform file:
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=.\SQLEXPRESS;Database=msmri_Users;UID=myuser;pwd=mypass;"
providerName="System.Data.SqlClient" xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<appSettings>
<add key="TableStorageEndpoint" value="http://127.0.0.1:10002/devstoreaccount1" xdt:Transform="Remove" xdt:Locator="Match(key)" />
</appSettings>
...
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.connection_string" xdt:Transform="Replace开发者_运维百科" xdt:Locator="Match(name)">
Data Source=.\SQLEXPRESS;Database=mydb;UID=myuser;pwd=mypass;
</property>
</session-factory>
</hibernate-configuration>
</configuration>
All ideas appreciated. Thanks!
The trick is to add the xml namespaces to the transform configs.
Here's an example setup of the Web.config:
<section name="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
...
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
...
<property name="show_sql">true</property>
...
</session-factory>
</hibernate-configuration>
Now add the xml namespace to your transformation config. (eg Web.Release.config):
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
xmlns:nhib="urn:nhibernate-configuration-2.2">
...
<nhib:hibernate-configuration>
<nhib:session-factory>
<nhib:property name="show_sql" xdt:Locator="Match(name)" xdt:Transform="SetAttributes">false</nhib:property>
</nhib:session-factory>
</nhib:hibernate-configuration>
The code should be replaced the way you want now.
Well, this won't work for all situations, but based on my comment above, if this really is not supported, my solutions is to add the connection string under connectionStrings and reference it from the hibernate config section. Then my transform is still being done within the one of the default config sections. Would still love to hear that this isn't a real limitation.
Late Update: So, the problem here is with sections containing an xmlns attribute - the config transform won't handle those. The workaround in some cases (e.g., with the assemblyBinding section), where there is a containing element, is to use Transform="Replace" on the parent element, like so:
<runtime xdt:Transform="Replace">
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="MySql.Data"
fullName="MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d">
</qualifyAssembly>
</assemblyBinding>
</runtime>
This still doesn't work for my nhibernate section, whose only parent is the configuration element itself, but . . .
精彩评论