web.config transformations not applied to special section - nhibernate

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

Related

Entity Framework, Custom Control, and No connection string named x, Still Runs

I recently rewrote a large program that my company uses to conduct business. I changed to Entity Framework early on. For the most part it has been working quite well.
One issue that has annoyed me for quite some time and now I have time to look into is, a custom control that pulls a question from the database causes a strange error.
When the custom control used SQL it would actually show the question on form display in the designer. At first this was pretty cool and I liked it. After switching it to use Entity Framework 6, when I load the form in the designer I get the error
No connection string named 'wotcDB' could be found in the application
config file.
It's totally in the app.config file. In fact the program has no problem compiling and running without issue. For the longest time I would just push Ignore and go to work on the form. The question of course no longer shows up in the designer.
The User Control is part of the application. The app.config file is there, and the program both runs and deploys fine. What's causing this?
Here is my app.config, ####### is sensitive data.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="webDB" connectionString="metadata=res://*/Data.webDB.csdl|res://*/Data.webDB.ssdl|res://*/Data.webDB.msl;provider=MySql.Data.MySqlClient;provider connection string="user id=#######;password=#######;server=#######;database=#######;persistsecurityinfo=True""
providerName="System.Data.EntityClient" />
<add name="wotcDB" connectionString="metadata=res://*/Data.wotcDB.csdl|res://*/Data.wotcDB.ssdl|res://*/Data.wotcDB.msl;provider=System.Data.SqlClient;provider connection string="data source=#######;initial catalog=#######;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider></providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data></configuration>
Rather than deal with the issue, since only I see it. I've found an answer at
This Stack Article
I put
If (System.ComponentModel.LicenseManager.UsageMode = System.ComponentModel.LicenseUsageMode.Designtime) Then
Load_Question()
End If
in the custom control load. I don't see the question during design time, which is fine for me. My other option was to put the connection string into the EF class, which is difficult to keep up since I'm using EF-Design and not code first.

Sql connection string for another pc

I have an application that runs on SQL 2005 database
I m trying to use same application on another machine same network,
I managed to log in on SQL 2005 using IP
Now I need to configure applications app.config file (found in visual studio solution) to allow connection
soo far I tried this connection string which isn't worked
this is what my config file looks like
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=ipaddress,1433;Initial Catalog=sample;User ID=sample;Password=sample;Trusted_Connection=True;" />
</appSettings>
</configuration>
Any help would be appreciated
Add this to your config file. Rather then using an appSetting use the actual connectionStrings section.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=00.000.0.00,1433;Initial Catalog=taxi;Persist Security Info=True;User ID=sample;Password=sample; providerName="System.Data.SqlClient" />
</connectionStrings>
This string should work if anyone else got the same problem
thanks everyone for looking and trying to figure it out with me
<add key="ConnectionString" value="Data Source=00.00.000.000;Initial Catalog=sample;User ID=sample;Password=sample;" />

Setting up NHibernate Velocity Cache on Azure

I am attempting to set up 2nd level caching for NHibernate 3.2 with Windows Azure Caching. So far, using https://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/, I have setup Azure caching in my web.config :
<section name="dataCacheClients" type="Microsoft.ApplicationServer.Caching.DataCacheClientsSection, Microsoft.ApplicationServer.Caching.Core" allowLocation="true" allowDefinition="Everywhere" />
...
<dataCacheClients>
<dataCacheClient name="default">
<autoDiscover isEnabled="true" identifier="App.UI" />
</dataCacheClient>
I then downloaded and added the Velocity cache provider DLLs from http://sourceforge.net/projects/nhcontrib/files/NHibernate.Caches/3.2.0.GA_for_NH3.2.0GA/
Finally, my hibernate.cfg.xml is:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
...
<property name="cache.use_second_level_cache">true</property>
<property name="cache.use_query_cache" >true</property>
<property name="cache.provider_class">NHibernate.Caches.Velocity.VelocityProvider, NHibernate.Caches.Velocity</property>
</session-factory>
</hibernate-configuration>
UPDATE
Solved, the dependency issue, I now get an error
{"ErrorCode:\"dcacheClient\" tag not specified in the application configuration file. Specify valid tag in configuration file."}
Thank you

Connection String in a VB.NET Application

I used to work only with asp.net websites before. I know that in ASP.NET website, the connection string is found in the web.config file.
My problem is now I have started working with VB.NET applications which needed to be interfaced to a database. How is a connection string created and where should I put it?
Thanks!
Here's the whole app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbAsthmaConnectionString" connectionString="Data Source=9300-00\SQLEXPRESS;Initial Catalog=dbStore;Persist Security Info=True;User ID=johnsmith;Password=1234" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog"/>
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog"
type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
initializeData="FileLogWriter"/>
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
</configuration>
Ok here is an axample :
1- Your app.config should look like this :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Amlakconn" connectionString ="Data Source=KHASHAYAR-PC\SQLEXPRESS;Initial Catalog=Amlak;Integrated Security=True"/>
</connectionStrings>
</configuration>
2- and in your code you can access the connectionsttring this way :
private string conn = ConfigurationManager.ConnectionStrings["Amlakconn"].ConnectionString;
go try It ;)
The other answers tell you where to put the connection string: http://msdn.microsoft.com/en-us/library/ms254494(v=vs.80).aspx
This is the easiest way to create a Connection String.
a) Create a Text file, rename the extension from TXT to UDL, press enter.
b) Double click the UDL file and choose OLEDB Provider For SQL Server > Next > type the Database Server name > Select the database and click Test Connection.
c) When the Test passes, close the UDL file and open it with notepad, the bold lines are the connection string:
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=YourDatabase;Data Source=SQLEXPRESS
If you are working on a webapp project It's the same ! you can put that in the web.config file , and if you project is win app you can add an app.config file to your project and pu the connection string in there !
Non ASP.NET application can also use config file, it is just not named web.config. This file has exactly the same structure you know from ASP.NET including ConnectionStrings section. You can copy content from web.config and paste sections you need into app.config. In project it will show as app.config but in the bin folder it is named after the executable file name (with exe extension) plus ".config".
To create this file, go to project's Add -> New Item and select Application Configuration File. To get access to your connection strings create/copy <ConnectionString> section into <configuration> section and then use this code:
string conStr = ConfigurationManager.ConnectionStrings["ConStringName"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Users", conStr);
You need to add reference to `System.Configuration' assembly.

ICriteria SetCacheable expiration time

I want to know how make NHibernate store my queries at 2-nd level cache for specified time
I see it only for entities cache.
Thank you for replying.
The query cache is not enabled by default. To enable it in your hibernate.cfg.xml:
<add key="hibernate.cache.use_query_cache" value="true" />
You should specify a cache region for queries. If not specified, the region will be "NHibernate.Cache.StandardQueryCache".
Session.CreateCriteria<User>()
.SetCacheRegion("UserQuery")
.List();
For syscache, cache regions are configured in you app.config:
<configuration>
<configSections>
<section name="syscache" type="NHibernate.Caches.SysCache.SysCacheSectionHandler,NHibernate.Caches.SysCache" />
</configSections>
<syscache>
<cache region="User" expiration="300" priority="3" />
<cache region="UserQuery" expiration="60" priority="3" />
</syscache>
</configuration>